B. Merlininice’s Hometask
Time Limit : 5000/2000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 59 Accepted Submission(s) : 26
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Merlininice dosen’t love math lessons, so he always ditches math class. But as the final exam is coming, now Merlininice is really regret his actions and wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Merlininice want to solve the task immediately. Can you help this poor guy?
You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.
Each digit is allowed to occur in the number the same number of times it occurs in the set.
![](http://rexdf.org/photo/Merlininice.jpg)
You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.
Each digit is allowed to occur in the number the same number of times it occurs in the set.
![](http://rexdf.org/photo/Merlininice.jpg)
Input
Input contains multiple test cases. For each test case: a single line contains a single integer n (1≤n≤100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
Output
On a first line print “Case #k:”, k is the k-th case. And on the second line, first print the number n, following “:”, and the the answer to the problem. If such number does not exist, then you should print -1.
***HINT: In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. ***
***HINT: In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. ***
Sample Input
1 0 11 3 4 5 4 5 3 5 3 4 4 0 8 3 2 5 1 5 2 2 3
Sample Output
Case #1: 1 : 0 Case #2: 11 : 5554443330 Case #3: 8 : -1
Author
Source
developing schools contest 5