Candy Redistribution

AtCoder
IDabc432_f
Time2000ms
Memory256MB
Difficulty
There are $N$ children, numbered $1$ to $N$. Child $i$ has $A_i$ candies. Your goal is to execute the following operation as few times as possible so that eventually all $N$ children have the same number of candies. * Choose two distinct children $x,y$, and choose a positive integer $z$ not exceeding the number of candies child $x$ has. Take $z$ candies from child $x$ and give them to child $y$. Determine whether there exists a sequence of operations such that eventually all $N$ children have the same number of candies. If it exists, find one such sequence with the minimum number of operations. ## Constraints * $2 \leq N \leq 20$ * $1 \leq A_i \leq 10^8$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $A_1$ $\dots$ $A_N$ [samples]
Samples
Input #1
4
1 7 4 8
Output #1
3
2 3 1
2 4 1
4 1 4

In this sample output, the operations are executed as follows:

1.  Take $1$ candy from child $2$ and give it to child $3$. Immediately after this, the number of candies each child has is $1,6,5,8$.
2.  Take $1$ candy from child $2$ and give it to child $4$. Immediately after this, the number of candies each child has is $1,5,5,9$.
3.  Take $4$ candies from child $4$ and give them to child $1$. Immediately after this, the number of candies each child has is $5,5,5,5$.

Eventually, all $N$ children have exactly five candies each.
Input #2
2
100 3
Output #2
\-1
API Response (JSON)
{
  "problem": {
    "name": "Candy Redistribution",
    "description": {
      "content": "There are $N$ children, numbered $1$ to $N$. Child $i$ has $A_i$ candies. Your goal is to execute the following operation as few times as possible so that eventually all $N$ children have the same num",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc432_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There are $N$ children, numbered $1$ to $N$.\nChild $i$ has $A_i$ candies.\nYour goal is to execute the following operation as few times as possible so that eventually all $N$ children have the same num...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments