Codeforce 214 Div 2 B. Hometask

Source: Internet
Author: User

Description:

Description

Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. but now Furik wants to get a good mark for math. for that Ms. ivanova, his math teacher, gave him a new task. furik solved the task immediately. can you?

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 by2, 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.

Input

A single line contains a single integerN(1? ≤?N? ≤? 100000)-the number of digits in the set. The second line containsNDigits, the digits are separated by a single space.

Output

On a single line print the answer to the problem. If such number does not exist, then you shoshould print-1.

Sample Input

Input
10
Output
0
Input
113 4 5 4 5 3 5 3 4 4 0
Output
5554443330
Input
83 2 5 1 5 2 2 3
Output
-1

This is the maximum number of n numbers that can be divisible by 2, 3, and 5.


Ideas:

This question was originally used to train hands quickly. I didn't expect it to be an afternoon. Finally, I barely AC after reading BMan's code. But after reading BMan's code, I really learned a lot! For example, I learned that CF has an ONLINE_JUDGE macro variable. You can use Conditional compilation to facilitate test setup and submission! The most important thing is to learn the idea, that is, the beauty of preprocessing!

The specific method is: because the answer is to be divisible by 3, the sum of the numbers must be a multiple of 3! If yes, direct output; otherwise, there are two possibilities:

1. sum % 3 = 1. In this case, you only need to remove the number of num [I] % 3 = 1 in the sequence, if not, find two num [] % 3 = 2!

2. sum % 3 = 2. This situation is basically the same as the previous one, so we will not repeat it here!


This question is prone to errors, that is, when determining the leading 0, BMan provides a very powerful practice. For specific practices, see the code implementation:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Define N 1000010 using namespace std; void debug () {# ifdef ONLINE_JUDGE # else freopen ("in.txt", "r", stdin ); # endif // JUDGE_ONLINE} int main () {debug (); // freopen ("in.txt", "r", stdin); int n; while (scanf ("% d", & n )! = EOF) {int num [N] = {0}; int vis [N]; int sum = 0; memset (vis, 0, sizeof (vis )); for (int I = 0; I <n; I ++) {scanf ("% d", & num [I]); sum + = num [I];} sort (num, num + n, greater
       
         (); If (sum % 3 = 1) {for (int I = n-1; I> = 0; I --) {if (num [I] % 3 = 1) {vis [I] = 1; sum-= num [I]; break ;}} int two = 0; if (sum % 3 = 1) // The num [I] % 3 = 1 cannot be found {for (int I = n-1; I> = 0; I --) {if (num [I] % 3 = 2) {vis [I] = 1; if (++ two >=2) {break ;}}} if (two! = 2) {printf ("-1 \ n"); continue ;}} if (sum % 3 = 2) {for (int I = n-1; i> = 0; I --) {if (num [I] % 3 = 2) {vis [I] = 1; sum-= num [I]; break ;}} int one = 0; if (sum % 3 = 2) {for (int I = n-1; I> = 0; I --) {if (num [I] % 3 = 1) {vis [I] = 1; if (++ one> = 2) {break ;}} if (one! = 2) {printf ("-1 \ n"); continue ;}} int cnt = 0; for (int I = 0; I <n; I ++) {if (! Vis [I]) {num [cnt ++] = num [I] ;}} n = cnt; if (n = 0 | num [n-1]! = 0) {printf ("-1");} else {int I = 0; while (I <n-1 & num [I] = 0) // judge the leading 0. If num [0] is equal to 0 at the beginning, it must be 0 at the end of the order because it has been sorted in descending order, in this way, the combination with the subsequent Code ensures that only one 0 {I ++;} for (; I <n; I ++) is output for the leading 0) {printf ("% d", num [I]) ;}} putchar (10);} return 0 ;}
       
      
     
    
   
  
 

Through today's study, I found that learning the code of the great gods is of great help to improve their coding capabilities! Look at other people's code !!!

One Day One Step!

 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.