CodeForces - 214B
2017-07-26 10:52
387 查看
yxc loves math lessons very much, so he doesn’t attend them, unlike wwl. But now yxc wants to get a good mark for math. For that Ms. lxh, his ACM teacher, gave him a new task. yxc 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 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.
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.
Example
Input
1
0
Output
0
Input
11
3 4 5 4 5 3 5 3 4 4 0
Output
5554443330
Input
8
3 2 5 1 5 2 2 3
Output
-1
Note (You Will Win)
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.
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.
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.
Example
Input
1
0
Output
0
Input
11
3 4 5 4 5 3 5 3 4 4 0
Output
5554443330
Input
8
3 2 5 1 5 2 2 3
Output
-1
Note (You Will Win)
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.
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; bool cmp(int x,int y){ return x>y; } int a[100010]; int b[100010]; int c[100010]; int main(){ int n; int sum=0; scanf("%d",&n); for (int i=0;i<n;i++){ scanf("%d",&a[i]); sum+=a[i]; } sort(a,a+n,cmp); if (sum%3==1){ int flag=0; for (int i=n-1;i>=0;i--){ if (a[i]%3==1){ b[i]=1; flag=1; break; } } if (flag==0){ int xx=0; if(sum%3==1){ for (int i=n-1;i>=0;i--){ if (a[i]%3==2){ b[i]=1; xx++; if (xx==2){ break; } } } if (xx!=2){ printf("-1\n"); return 0; } } } } if (sum%3==2){ int flag=0; for (int i=n-1;i>=0;i--){ if (a[i]%3==2){ b[i]=1; flag=1; break; } } if (flag==0){ int xx=0; if(sum%3==2){ for (int i=n-1;i>=0;i--){ if (a[i]%3==1){ b[i]=1; xx++; if (xx==2){ break; } } } if (xx!=2){ printf("-1\n"); return 0; } } } } int xx=0; for (int i=0;i<n;i++){ if (b[i]!=1){ c[xx++]=a[i]; } } if (xx==0||c[xx-1]!=0){ printf("-1\n"); } else{ int i=0; while(i<xx-1&&c[i]==0){ i++; } for (;i<xx;i++){ printf("%d",c[i]); } printf("\n"); } return 0; }
相关文章推荐
- CodeForces 214B
- codeforces 214B Hometask
- codeforces 214B - Hometask
- CodeForces 214B Hometask
- Codeforces 214B Hometask
- Hometask--codeForces 214B
- codeforces 214B
- [codeforces 891E]Lust
- Codeforces 792 E. Colored Balls
- codeforces 833E. Caramel Clouds
- 【1】【组合数学】CodeForces 650A Watchmen
- CodeForces 471C - MUH and House of Cards(推导)
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
- CodeForces - 906D Power Tower(欧拉降幂定理)
- Playing with String(codeforces 305E)
- CodeForces 493 E.Vasya and Polynomial(数学)
- 【1】【数学】CodeForces 588C Duff and Weight Lifting
- codeforces 148 D. Bag of mice 概率dp
- Codeforces 609C Load Balancing 【水题】
- codeforces 913B Christmas Spruce+链式前向星