#K85032. Sum of Digits of the Largest Divisible by 3

    ID: 36551 Type: Default 1000ms 256MiB

Sum of Digits of the Largest Divisible by 3

Sum of Digits of the Largest Divisible by 3

Given an integer \(n\) and a list of \(n\) integers, find the largest integer \(x\) that is divisible by 3 (i.e. \(x \mod 3 = 0\)). If such an integer exists, compute the sum of its digits; otherwise, output -1.

The task can be formalized as follows: Let \(L\) be the list of integers. Find \(x = \max\{a \in L \mid a \mod 3 = 0\}\). If \(x\) exists, calculate \(S(x)\), where \(S(x)\) denotes the sum of the digits of \(x\). If no such \(x\) exists, output \(-1\).

inputFormat

The input consists of two lines:

  1. The first line contains an integer \(n\) denoting the number of integers.
  2. The second line contains \(n\) space-separated integers.

outputFormat

Output a single integer representing the sum of the digits of the largest integer divisible by 3, or -1 if no such integer exists.

## sample
5
12 15 7 20 9
6

</p>