#604
Unrated
Mana Collection
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Note: The time limit for this problem is 5s, 2.5 times the default. The memory limit for this problem is 512MB, twice the default.

Bessie has recently taken an interest in magic and needs to collect mana for a very important spell. Bessie has NN (1N181\le N\le 18) mana pools, the iith of which accumulates mim_i mana per second (1mi1081\le m_i\le 10^8). The pools are linked by a collection of MM (0MN(N1)0\le M\le N(N-1)) directed edges (ai,bi,ti)(a_i,b_i,t_i), meaning that she can travel from aia_i to bib_i in tit_i seconds (1ai,biN1\le a_i, b_i\le N, aibia_i\neq b_i, 1ti1091\le t_i\le 10^9). Whenever Bessie is present at a pool, she can collect all the mana stored at that location, emptying it. At time 00, all mana pools are empty, and Bessie can select any pool to start at.

Answer QQ (1Q21051\le Q\le 2\cdot 10^5) queries, each specified by two integers ss and ee (1s1091\le s\le 10^9, 1eN1\le e\le N). For each query, determine the maximum amount of mana Bessie can collect in ss seconds if she must be at mana pool ee at the end of the ssth second.

입력

First line contains NN and MM.

Next line contains m1,m2,,mNm_1,m_2,\dots, m_N.

Next MM lines contain ai,bi,tia_i,b_i,t_i. No ordered pair (ai,bi)(a_i,b_i) appears more than once in the input.

Next line contains QQ.

Next QQ lines contain two integers ss and ee.

출력

QQ lines, one for each query.

예제 입력 1

2 1
1 10
1 2 10
4
5 1
5 2
100 1
100 2

예제 출력 1

5
50
100
1090

예제 입력 2

4 8
50000000 100000000 20000000 70000000
1 2 20
2 1 50
2 3 90
1 3 40
3 1 10
4 1 25
1 4 5
4 3 70
3
8 3
1000000000 1
500000 4

예제 출력 2

160000000
239999988050000000
119992550000000

점수

Inputs 3-4: N10,Q100N\le 10, Q\le 100Inputs 5-9: N10N\le 10Inputs 10-14: Q100Q\le 100Inputs 15-17: N=16N = 16Inputs 18-20: N=17N = 17Inputs 21-24: No additional constraints.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.