A

来源:互联网 发布:枕头什么牌子好 知乎 编辑:程序博客网 时间:2024/05/17 03:48

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 contains n integers a1, a2, ..., an (ai = 0 or ai = 5). Number airepresents the digit that is written on the i-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

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.

 

解题思路:要想被9整除,应该是9的倍数个5,被90整除后面再加0就行

代码:

#include<iostream>using namespace std;int main(){    int n,i,a;    while(cin>>n)    {        int num0=0;        int num5=0;        for(i=0;i<n;i++)        {            cin>>a;            if(a==0) num0++;            if(a==5) num5++;        }        if(num0==0) cout<<-1<<endl;        else {            if(num5<9) cout<<0<<endl;            else if(num5>=9)            {                for(i=0;i<(num5/9)*9;i++)                cout<<5;                for(i=0;i<num0;i++)                cout<<0;                cout<<endl;            }            }    }    return 0;}


 

 

0 0
原创粉丝点击