JeffandDigits

来源:互联网 发布:js导出excel 数字格式 编辑:程序博客网 时间:2024/06/05 20:06

Description

Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got?

Jeff must make the number without leading zero. At that, we assume that number 0 doesn't contain any leading zeroes. Jeff doesn't have to use all the cards.

Input

The first line contains integer n(1 ≤ n ≤ 103). The next line containsn integers a1,a2,..., an (ai = 0 or ai = 5). Numberai represents the digit that is written on thei-th card.

Output

In a single line print the answer to the problem — the maximum number, divisible by 90. If you can't make any divisible by 90 number from the cards, print -1.

Sample Input

Input
45 0 5 0
Output
0
Input
115 5 5 5 5 5 5 5 0 5 5
Output
5555555550

Sample Output

Hint

In the first test you can make only one number that is a multiple of 90 —0.

In the second test you can make number 5555555550, it is a multiple of 90.


#include<iostream>
#include<algorithm>
using namespace std;
int b[10000];
int main()
{
   long long n,i,s,t,l;
   while(cin>>n)
   {
       s=0;l=0;
       for(i=0;i<n;i++)
       cin>>b[i];
       sort(b,b+n);
       if(b[0])
       cout<<"-1"<<endl;
       else
       {
           for(i=0;i<n;i++)
           {
               if(b[i])
               s++;
               else
               l++;
           }
           t=s/9;
           for(i=1;i<=t*9;i++)
           cout<<"5";
           if(t==0)
           cout<<"0";
           else
           for(i=1;i<=l;i++)
           cout<<"0";
           cout<<endl;
       }
   }
}
9的倍数个5组合才能被9整除t=0
是只能输出一个0


0 0
原创粉丝点击