Codeforces Round #131 (Div. 2)

来源:互联网 发布:lte网络架构图 编辑:程序博客网 时间:2024/06/04 17:46
B. Hometask
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 by 235 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 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 single line print the answer to the problem. If such number does not exist, then you should print -1.

Sample test(s)
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
Note

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.

题意很清晰,给出N个数字,问用这些数字组成的数模30余0的数字,最大是多少。

大家都知道模3余0的定义是数字之和加起来模3为0,而我们要求的模30余0的数字,所以最后组成的数字必须含0,故不含0的必然为-1;

而最后得到的数字则需要注意前导0就行了

#include<iostream>#include<cmath>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>using namespace std;bool cmp(int a,int b){return a>b;}const int maxn=1000000;int a[maxn];int n;vector<int> one,two,zero;int couts(int x,int y){bool r=0;for(int i=0;i<n;i++){if(i==x||i==y)continue;if(a[i]==0){if(r)cout<<a[i];}else{r=1;cout<<a[i];}}if(!r)cout<<0;cout<<endl;}int main(){while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n,cmp);int f=0;int sum=0;for(int i=0;i<n;i++){sum+=a[i];if(a[i]%3==0){if(a[i]==0)f=1;zero.push_back(i);//将数字的序号而不是数字本身存入vector,因为数字一共只有10个                 //在后面输出的时候会出现重复,而序列号则不会。  }else if(a[i]%3==1)one.push_back(i);else if(a[i]%3==2)two.push_back(i);}if(f==0){cout<<"-1"<<endl;return 0;}else{if(sum%3==0)couts(-1,-1);else if(sum%3==1){if(one.size()>0)couts(one.back(),-1);else if(two.size()>0)couts(two[two.size()-1],two[two.size()-2]);}else if(sum%3==2){if(two.size()>0)couts(two.back(),-1);else if(one.size()>0)couts(one[one.size()-1],one[one.size()-2]);}}}}

原创粉丝点击