codeforces-scrape

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

1637C.md (2660B)


      1 Source: https://codeforces.com/problemset/problem/1637/C
      2 
      3 C. Andrew and Stones
      4 
      5 time limit per test
      6 
      7 1 second
      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 Andrew has $n$ piles with stones. The $i$-th pile contains $a_i$ stones. He wants to make his table clean so he decided to put every stone either to the $1$-st or the $n$-th pile.
     22 
     23 Andrew can perform the following operation any number of times: choose $3$ indices $1 \le i \lt j \lt k \le n$, such that the $j$-th pile contains at least $2$ stones, then he takes $2$ stones from the pile $j$ and puts one stone into pile $i$ and one stone into pile $k$.
     24 
     25 Tell Andrew what is the minimum number of operations needed to move all the stones to piles $1$ and $n$, or determine if it's impossible.
     26 
     27 Input
     28 
     29 The input contains several test cases. The first line contains one integer $t$ ($1 \leq t \leq 10\,000$) — the number of test cases.
     30 
     31 The first line for each test case contains one integer $n$ ($3 \leq n \leq 10^5$) — the length of the array.
     32 
     33 The second line contains a sequence of integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the array elements.
     34 
     35 It is guaranteed that the sum of the values $n$ over all test cases does not exceed $10^5$.
     36 
     37 Output
     38 
     39 For each test case print the minimum number of operations needed to move stones to piles $1$ and $n$, or print $-1$ if it's impossible.
     40 
     41 Example
     42 
     43 Input
     44 
     45 ```
     46 4
     47 
     48 5
     49 
     50 1 2 2 3 6
     51 
     52 3
     53 
     54 1 3 1
     55 
     56 3
     57 
     58 1 2 1
     59 
     60 4
     61 
     62 3 1 1 2
     63 ```
     64 
     65 Output
     66 
     67 ```
     68 4
     69 -1
     70 1
     71 -1
     72 ```
     73 
     74 Note
     75 
     76 In the first test case, it is optimal to do the following:
     77 
     78 1. Select $(i, j, k) = (1, 2, 5)$. The array becomes equal to $[2, 0, 2, 3, 7]$.
     79 2. Select $(i, j, k) = (1, 3, 4)$. The array becomes equal to $[3, 0, 0, 4, 7]$.
     80 3. Twice select $(i, j, k) = (1, 4, 5)$. The array becomes equal to $[5, 0, 0, 0, 9]$. This array satisfy the statement, because every stone is moved to piles $1$ and $5$.
     81 
     82 There are $4$ operations in total.
     83 
     84 In the second test case, it's impossible to put all stones into piles with numbers $1$ and $3$:
     85 
     86 1. At the beginning there's only one possible operation with $(i, j, k) = (1, 2, 3)$. The array becomes equal to $[2, 1, 2]$.
     87 2. Now there is no possible operation and the array doesn't satisfy the statement, so the answer is $-1$.
     88 
     89 In the third test case, it's optimal to do the following:
     90 
     91 1. Select $(i, j, k) = (1, 2, 3)$. The array becomes equal to $[2, 0, 2]$. This array satisfies the statement, because every stone is moved to piles $1$ and $3$.
     92 
     93 The is $1$ operation in total.
     94 
     95 In the fourth test case, it's impossible to do any operation, and the array doesn't satisfy the statement, so the answer is $-1$.