Banned X 2

AtCoder
IDarc211_a
Time2000ms
Memory256MB
Difficulty
You are given a length-$9$ sequence of non-negative integers $A=(A_1,A_2,\dots,A_9)$. It is guaranteed that $2\leq \sum_{i=1}^{9}A_i$. You can perform the following operation any number of times (possibly zero): choose one element of $A$ and add $1$ to it. What is the minimum number of operations required so that there exists a sequence of positive integers $S$ satisfying all of the following conditions? * All elements of $S$ are between $1$ and $9$, inclusive. * $S$ contains exactly $A_i$ occurrences of $i$ for each integer $i$ such that $1\leq i\leq 9$. (Thus, the length of $S$ is $\sum_{i=1}^{9}A_i$.) * No two adjacent elements sum to $10$. It can be proved that the goal can be achieved in a finite number of operations. You are given $T$ test cases; solve each of them. ## Constraints * $1\leq T\leq 10^3$ * $0\leq A_i\leq 10^9$ $(1\leq i\leq 9)$ * $2\leq \sum_{i=1}^{9}A_i$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $T$ $\text{case}_1$ $\text{case}_2$ $\vdots$ $\text{case}_T$ Each test case is given in the following format: $A_1$ $A_2$ $\dots$ $A_9$ [samples]
Samples
Input #1
2
0 0 0 1 0 1 0 0 0
2 1 1 0 0 0 0 0 0
Output #1
1
0

For the first test case, initially, the possible values of $S$ satisfying all conditions except the last one are $(4,6)$ and $(6,4)$, neither of which satisfies the last condition. By choosing $A_8$ and adding $1$ to it, $S=(6,8,4)$ satisfies all conditions.
For the second test case, $S=(1,2,3,1)$ satisfies the conditions without any operations.
API Response (JSON)
{
  "problem": {
    "name": "Banned X 2",
    "description": {
      "content": "You are given a length-$9$ sequence of non-negative integers $A=(A_1,A_2,\\dots,A_9)$. It is guaranteed that $2\\leq \\sum_{i=1}^{9}A_i$. You can perform the following operation any number of times (poss",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc211_a"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a length-$9$ sequence of non-negative integers $A=(A_1,A_2,\\dots,A_9)$. It is guaranteed that $2\\leq \\sum_{i=1}^{9}A_i$.\nYou can perform the following operation any number of times (poss...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments