Score : 500 points
Problem Statement
There are integer sequences A,B,C of length M each.
C is represented by integers x1,…,xN,y1,…,yN. The first y1 terms of C are x1, the subsequent y2 terms are x2, …, the last yN terms are xN.
B is defined by Bi=∑k=1iCk(1≤i≤M).
A is defined by Ai=∑k=1iBk(1≤i≤M).
Find the maximum value among A1,…,AM.
You will be given T test cases to solve.
Constraints
- 1≤T≤2×105
- 1≤N≤2×105
- The sum of N in a single file is at most 2×105.
- 1≤M≤109
- ∣xi∣≤4(1≤i≤N)
- yi>0(1≤i≤N)
- ∑k=1Nyk=M
- All values in input are integers.
Input is given from Standard Input in the following format:
T
case1
⋮
caseT
Each case is in the following format:
N M
x1 y1
⋮
xN yN
Output
Print T lines. The i-th line (1≤i≤T) should contain the answer to the i-th test case.
3
3 7
-1 2
2 3
-3 2
10 472
-4 12
1 29
2 77
-1 86
0 51
3 81
3 17
-2 31
-4 65
4 23
1 1000000000
4 1000000000
4
53910
2000000002000000000
In the first test case, we have:
- C=(−1,−1,2,2,2,−3,−3)
- B=(−1,−2,0,2,4,1,−2)
- A=(−1,−3,−3,−1,3,4,2)
Thus, the maximum value among A1,…,AM is 4.