Benchmark AI / Public workspace

LiveCodeBench / 1899_B / B. 250 Thousand Tons of TNT

Problem

Answer published by the source. Consult the official source to check your work against its answer.

contest date

2023-10-17T00:00:00

contest id

1899

difficulty

hard

platform

codeforces

question content

Alex is participating in the filming of another video of BrMeast, and BrMeast asked Alex to prepare 250 thousand tons of TNT, but Alex didn't hear him well, so he prepared nn boxes and arranged them in a row waiting for trucks. The ii-th box from the left weighs aia_i tons. All trucks that Alex is going to use hold the same number of boxes, denoted by kk. Loading happens the following way: - The first kk boxes goes to the first truck, - The second kk boxes goes to the second truck, - $\dotsb$ - The last kk boxes goes to the nk\frac{n}{k}-th truck. Upon loading is completed, each truck must have exactly kk boxes. In other words, if at some point it is not possible to load exactly kk boxes into the truck, then the loading option with that kk is not possible. Alex hates justice, so he wants the maximum absolute difference between the total weights of two trucks to be as great as possible. If there is only one truck, this value is 00. Alex has quite a lot of connections, so for every 1kn1 \leq k \leq n, he can find a company such that each of its trucks can hold exactly kk boxes. Print the maximum absolute difference between the total weights of any two trucks. Input The first line contains one integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. The first line of each test case contains one integer nn (1n1500001 \leq n \leq 150\,000) — the number of boxes. The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1091 \leq a_i \leq 10^9) — the weights of the boxes. It is guaranteed that the sum of nn for all test cases does not exceed 150000150\,000. Output For each test case, print a single integer — the answer to the problem.Sample Input 1: 5 2 1 2 6 10 2 3 6 1 3 4 1000000000 1000000000 1000000000 1000000000 15 60978 82265 78961 56708 39846 31071 4913 4769 29092 91348 64119 72421 98405 222 14294 8 19957 69913 37531 96991 57838 21008 14207 19198 Sample Output 1: 1 9 0 189114 112141 Note In the first case, we should pick two trucks, so the first one will have only the first box, and the second one will have only the second box. In the second case, we should pick six trucks, so the maximum will be 1010, the minimum will be 11, and the answer is 101=910 - 1 = 9. In the third case, for any possible kk, the trucks will have the same total weight of boxes, so the answer is 00.
Plain-text mathematical notation (without MathML)
Alex is participating in the filming of another video of BrMeast, and BrMeast asked Alex to prepare 250 thousand tons of TNT, but Alex didn't hear him well, so he prepared n boxes and arranged them in a row waiting for trucks. The i-th box from the left weighs a_(i) tons.

All trucks that Alex is going to use hold the same number of boxes, denoted by k. Loading happens the following way:

 
-  The first k boxes goes to the first truck, 
-  The second k boxes goes to the second truck, 
-  $\dotsb$ 
-  The last k boxes goes to the (n)/(k)-th truck. Upon loading is completed, each truck must have exactly k boxes. In other words, if at some point it is not possible to load exactly k boxes into the truck, then the loading option with that k is not possible.

Alex hates justice, so he wants the maximum absolute difference between the total weights of two trucks to be as great as possible. If there is only one truck, this value is 0.

Alex has quite a lot of connections, so for every 1≤k≤n, he can find a company such that each of its trucks can hold exactly k boxes. Print the maximum absolute difference between the total weights of any two trucks.

Input

The first line contains one integer t (1≤t≤10⁴) — the number of test cases.

The first line of each test case contains one integer n (1≤n≤150 000) — the number of boxes.

The second line contains n integers a₁,a₂,…,a_(n) (1≤a_(i)≤10⁹) — the weights of the boxes.

It is guaranteed that the sum of n for all test cases does not exceed 150 000.

Output

For each test case, print a single integer — the answer to the problem.Sample Input 1:
5

2

1 2

6

10 2 3 6 1 3

4

1000000000 1000000000 1000000000 1000000000

15

60978 82265 78961 56708 39846 31071 4913 4769 29092 91348 64119 72421 98405 222 14294

8

19957 69913 37531 96991 57838 21008 14207 19198



Sample Output 1:

1
9
0
189114
112141


Note

In the first case, we should pick two trucks, so the first one will have only the first box, and the second one will have only the second box.

In the second case, we should pick six trucks, so the maximum will be 10, the minimum will be 1, and the answer is 10−1=9.

In the third case, for any possible k, the trucks will have the same total weight of boxes, so the answer is 0.
Original LaTeX notation
Alex is participating in the filming of another video of BrMeast, and BrMeast asked Alex to prepare 250 thousand tons of TNT, but Alex didn't hear him well, so he prepared $n$ boxes and arranged them in a row waiting for trucks. The $i$-th box from the left weighs $a_i$ tons.

All trucks that Alex is going to use hold the same number of boxes, denoted by $k$. Loading happens the following way:

 
-  The first $k$ boxes goes to the first truck, 
-  The second $k$ boxes goes to the second truck, 
-  $\dotsb$ 
-  The last $k$ boxes goes to the $\frac{n}{k}$-th truck. Upon loading is completed, each truck must have exactly $k$ boxes. In other words, if at some point it is not possible to load exactly $k$ boxes into the truck, then the loading option with that $k$ is not possible.

Alex hates justice, so he wants the maximum absolute difference between the total weights of two trucks to be as great as possible. If there is only one truck, this value is $0$.

Alex has quite a lot of connections, so for every $1 \leq k \leq n$, he can find a company such that each of its trucks can hold exactly $k$ boxes. Print the maximum absolute difference between the total weights of any two trucks.

Input

The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.

The first line of each test case contains one integer $n$ ($1 \leq n \leq 150\,000$) — the number of boxes.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the weights of the boxes.

It is guaranteed that the sum of $n$ for all test cases does not exceed $150\,000$.

Output

For each test case, print a single integer — the answer to the problem.Sample Input 1:
5

2

1 2

6

10 2 3 6 1 3

4

1000000000 1000000000 1000000000 1000000000

15

60978 82265 78961 56708 39846 31071 4913 4769 29092 91348 64119 72421 98405 222 14294

8

19957 69913 37531 96991 57838 21008 14207 19198



Sample Output 1:

1
9
0
189114
112141


Note

In the first case, we should pick two trucks, so the first one will have only the first box, and the second one will have only the second box.

In the second case, we should pick six trucks, so the maximum will be $10$, the minimum will be $1$, and the answer is $10 - 1 = 9$.

In the third case, for any possible $k$, the trucks will have the same total weight of boxes, so the answer is $0$.

question title

B. 250 Thousand Tons of TNT

starter code

Code

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import