atcoder#ABC240F. [ABC240F] Sum Sum Max

[ABC240F] Sum Sum Max

Score : 500500 points

Problem Statement

There are integer sequences A,B,CA, B, C of length MM each.

CC is represented by integers x1,,xN,y1,,yNx_1, \dots, x_N, y_1, \dots, y_N. The first y1y_1 terms of CC are x1x_1, the subsequent y2y_2 terms are x2x_2, \ldots, the last yNy_N terms are xNx_N.

BB is defined by Bi=k=1iCk(1iM)B_i = \sum_{k = 1}^i C_k \, (1 \leq i \leq M).

AA is defined by Ai=k=1iBk(1iM)A_i = \sum_{k = 1}^i B_k \, (1 \leq i \leq M).

Find the maximum value among A1,,AMA_1, \dots, A_M.

You will be given TT test cases to solve.

Constraints

  • 1T2×1051 \leq T \leq 2 \times 10^5
  • 1N2×1051 \leq N \leq 2 \times 10^5
  • The sum of NN in a single file is at most 2×1052 \times 10^5.
  • 1M1091 \leq M \leq 10^9
  • xi4(1iN)|x_i| \leq 4 \, (1 \leq i \leq N)
  • yi>0(1iN)y_i \gt 0 \, (1 \leq i \leq N)
  • k=1Nyk=M\sum_{k = 1}^N y_k = M
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

TT

case1\mathrm{case}_1

\vdots

caseT\mathrm{case}_T

Each case is in the following format:

NN MM

x1x_1 y1y_1

\vdots

xNx_N yNy_N

Output

Print TT lines. The ii-th line (1iT)(1 \leq i \leq T) should contain the answer to the ii-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)C = (-1, -1, 2, 2, 2, -3, -3)
  • B=(1,2,0,2,4,1,2)B = (-1, -2, 0, 2, 4, 1, -2)
  • A=(1,3,3,1,3,4,2)A = (-1, -3, -3, -1, 3, 4, 2)

Thus, the maximum value among A1,,AMA_1, \dots, A_M is 44.