#abc240f. F - Sum Sum Max

F - Sum Sum Max

Score : 500500 points

问题描述

存在长度均为 MM 的整数序列 AA, BB, CC

CC 通过整数 x1,,xN,y1,,yNx_1, \dots, x_N, y_1, \dots, y_N 表示。其中,CC 的前 y1y_1 项均为 x1x_1,接下来的 y2y_2 项均为 x2x_2,依此类推,最后 yNy_N 项均为 xNx_N

根据以上定义,BB 序列由 Bi=k=1iCk(1iM)B_i = \sum_{k = 1}^i C_k \, (1 \leq i \leq M) 给出。

进一步地,AA 序列由 Ai=k=1iBk(1iM)A_i = \sum_{k = 1}^i B_k \, (1 \leq i \leq M) 定义。

请找出 A1,,AMA_1, \dots, A_M 中的最大值。

您将收到 TT 个测试用例需要解决。

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

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

Sample Output 1

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.

update @ 2024/3/10 10:21:47