#P6014. Card Game Scoring

    ID: 19238 Type: Default 1000ms 256MiB

Card Game Scoring

Card Game Scoring

Given n cards with values ranging from 1 to 10, you need to select n-2 cards whose sum is a multiple of 10. The score is determined as the last digit of the sum of the remaining two cards. In particular, if the sum of the two remaining cards is a multiple of 10, the score is 10 (known as Niuhonghong). If no selection of n-2 cards can form a sum that is a multiple of 10, then the score is 0 (known as Niu Bu Long).

Mathematically, let the total sum be \(S\). We want to partition the cards into two groups: one of size \(n-2\) and one of size 2, such that

[ S - (a+b) \equiv 0 \pmod{10}, ]

which is equivalent to

[ (a+b) \equiv S \pmod{10}. ]

If such a pair \((a,b)\) exists then the score is \(\text{score} = \begin{cases} 10, & \text{if } (a+b) \equiv 0 \pmod{10}, \\ (a+b) \bmod 10, & \text{otherwise.} \end{cases}\) Otherwise, the score is 0.

inputFormat

The first line contains an integer n representing the number of cards. The second line contains n space-separated integers representing the values of the cards.

outputFormat

Output a single integer — the score computed by the rules described above.

sample

5
1 2 3 4 5
5