#P8367. [LNOI2022] 盒

[LNOI2022] 盒

Problem Description

There are nn distinct boxes in a row. Initially, the ii-th box contains aia_i items, and the total number of items is S=i=1naiS = \sum_{i = 1}^{n} a_i. For a non-negative integer array (b1,b2,,bn)(b_1, b_2, \ldots, b_n) satisfying i=1nbi=S\sum_{i = 1}^{n} b_i = S, consider the following problem:

You want the ii-th box to contain exactly bib_i items. To achieve this, you may perform the following operation any number of times: for two adjacent boxes, move exactly one item from one box to the other. The cost of performing one operation on boxes ii and i+1i + 1 (1i<n1 \le i < n) is wiw_i. Note: moving one item from box i\bm{i} to box i+1\bm{i + 1} and moving one item from box i+1\bm{i + 1} to box i\bm{i} both cost wi\bm{w_i}. You must ensure that during the operations, no box ever has a negative number of items.

Under the above setting, define the minimum cost to reach a state where the ii-th box contains exactly bib_i items from the initial state as val(b1,b2,,bn)\operatorname{val}(b_1, b_2, \ldots, b_n). You need to compute, over all non-negative integer arrays (b1,b2,,bn)(b_1, b_2, \ldots, b_n) satisfying i=1nbi=S\sum_{i = 1}^{n} b_i = S, the sum of val(b1,b2,,bn)\operatorname{val}(b_1, b_2, \ldots, b_n). Output the answer modulo 998244353998244353.

Input Format

This problem has multiple test cases. The first line contains a positive integer TT, the number of test cases.

For each test case, the input consists of three lines. The first line contains a positive integer nn, the number of boxes. The second line contains nn non-negative integers a1,a2,,ana_1, a_2, \ldots, a_n describing the initial state. The third line contains n1n - 1 non-negative integers w1,w2,,wn1w_1, w_2, \ldots, w_{n - 1} describing the cost of moving items.

Output Format

For each test case, output one integer per line: the sum, over all non-negative integer arrays satisfying i=1nbi=S\sum_{i = 1}^{n} b_i = S, of the minimum cost to reach the target state, modulo 998244353998244353.

2
2
2 3
65472
5
1 3 2 1 1
2 3 3 3

589248
8589

Hint

[Sample Explanation #1]

For the first test case, there are six cases to consider. The pairs (b1,b2)(b_1, b_2) are (0,5),(1,4),(2,3),(3,2),(4,1),(5,0)(0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0).

In the first case, at least 22 moves are needed, and the minimum cost is 65472×2=13094465472 \times 2 = 130944.

In the second case, at least 11 move is needed, and the minimum cost is 6547265472.

In the third case, no operation is needed, and the minimum cost is 00.

In the fourth case, at least 11 move is needed, and the minimum cost is 6547265472.

In the fifth case, at least 22 moves are needed, and the minimum cost is 65472×2=13094465472 \times 2 = 130944.

In the last case, at least 33 moves are needed, and the minimum cost is 65472×3=19641665472 \times 3 = 196416.

Therefore, the sum of minimum costs is $130944 + 65472 + 0 + 65472 + 130944 + 196416 = 589248$.

[Sample #2]

See box/box2.in and box/box2.ans in the attachment.

This sample satisfies the constraints of test points 585 \sim 8.

[Sample #3]

See box/box3.in and box/box3.ans in the attachment.

This sample satisfies the constraints of test points 9129 \sim 12.

[Sample #4]

See box/box4.in and box/box4.ans in the attachment.

This sample satisfies the constraints of test points 131413 \sim 14.

[Sample #5]

See box/box5.in and box/box5.ans in the attachment.

This sample satisfies the constraints of test points 151615 \sim 16.

[Sample #6]

See box/box6.in and box/box6.ans in the attachment.

This sample satisfies the constraints of test points 171817 \sim 18.

[Constraints]

It is guaranteed that for every test point and every test case: 2n5×1052 \le n \le 5 \times {10}^5, 1S2×1061 \le S \le 2 \times {10}^6, ai0a_i \ge 0, and 0wi<9982443530 \le w_i < 998244353.

Test Point ID TT \le nn \le SS \le Special Properties
121 \sim 2 10001000 55 A
343 \sim 4 55 99 None
585 \sim 8 1010 20002000 20002000
9129 \sim 12 2×1052 \times {10}^5
131413 \sim 14 22 2×1052 \times {10}^5 B
151615 \sim 16 AC
171817 \sim 18 None
192019 \sim 20 55 5×1055 \times {10}^5 2×1062 \times {10}^6

Special Property A: For any 1i<n1 \le i < n, wi=1w_i = 1.
Special Property B: For any 1i<n201 \le i < n - 20, ai=0a_i = 0.
Special Property C: There are at most 2020 indices i[1,n]i \in [1, n] such that ai0a_i \ne 0.

[Hint]

Some test points have a large input size. To optimize runtime, it is recommended to use a faster input method.

Translated by ChatGPT 5