codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1899B.md (2578B)


      1 Source: https://codeforces.com/problemset/problem/1899/B
      2 
      3 B. 250 Thousand Tons of TNT
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 All trucks that Alex is going to use hold the same number of boxes, denoted by $k$. Loading happens the following way:
     24 
     25 * The first $k$ boxes goes to the first truck,
     26 * The second $k$ boxes goes to the second truck,
     27 * $\dotsb$
     28 * The last $k$ boxes goes to the $\frac{n}{k}$-th truck.
     29 
     30 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.
     31 
     32 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$.
     33 
     34 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.
     35 
     36 Input
     37 
     38 The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
     39 
     40 The first line of each test case contains one integer $n$ ($1 \leq n \leq 150\,000$) — the number of boxes.
     41 
     42 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.
     43 
     44 It is guaranteed that the sum of $n$ for all test cases does not exceed $150\,000$.
     45 
     46 Output
     47 
     48 For each test case, print a single integer — the answer to the problem.
     49 
     50 Example
     51 
     52 Input
     53 
     54 ```
     55 5
     56 
     57 2
     58 
     59 1 2
     60 
     61 6
     62 
     63 10 2 3 6 1 3
     64 
     65 4
     66 
     67 1000000000 1000000000 1000000000 1000000000
     68 
     69 15
     70 
     71 60978 82265 78961 56708 39846 31071 4913 4769 29092 91348 64119 72421 98405 222 14294
     72 
     73 8
     74 
     75 19957 69913 37531 96991 57838 21008 14207 19198
     76 ```
     77 
     78 Output
     79 
     80 ```
     81 1
     82 9
     83 0
     84 189114
     85 112141
     86 ```
     87 
     88 Note
     89 
     90 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.
     91 
     92 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$.
     93 
     94 In the third case, for any possible $k$, the trucks will have the same total weight of boxes, so the answer is $0$.