CF 214 B Hometask.cpp

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.

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

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.


简单的模拟,想到就很好做了。

首先2,5的倍数肯定以0结尾,其次也是3的倍数,则各个为相加和为3的倍数

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 100000+5;
int N,num,x;
int main(){
	vector<int> V[3];
	while(~scanf("%d",&N)){
		num = 0;
		for(int i = 0; i < 3; i++)
			V[i].clear();
		for(int i = 0; i < N; i++){
			scanf("%d",&x);
			if(x==0)
				num++;
			else{
				V[x%3].push_back(x);
			}
		}
		if(num == 0)
			printf("-1");
		else{
			sort(V[1].begin(),V[1].end());
			sort(V[2].begin(),V[2].end());
			vector<int> ans;
			for(int i = 0; i < V[0].size(); i++){
				ans.push_back(V[0][i]);
			}
			int size_1 = V[1].size();
			int size_2 = V[2].size();
			int temp = (size_1+size_2*2)%3;
			if(temp == 0){
				for(int i = 0; i < V[1].size(); i++)
					ans.push_back(V[1][i]);
				for(int i = 0; i < V[2].size(); i++)
					ans.push_back(V[2][i]);
			}
			else if(temp == 1){
				if(size_1 > 0){
					for(int i = 1; i < V[1].size(); i++)
						ans.push_back(V[1][i]);
					for(int i = 0; i < V[2].size(); i++)
						ans.push_back(V[2][i]);
				}
				else{
					for(int i = 0; i < V[1].size(); i++)
						ans.push_back(V[1][i]);
					for(int i = 2; i < V[2].size(); i++)
						ans.push_back(V[2][i]);
				}
			}
			else{
				if(size_2 > 0){
					for(int i = 0; i < V[1].size(); i++)
						ans.push_back(V[1][i]);
					for(int i = 1; i < V[2].size(); i++)
						ans.push_back(V[2][i]);
				}
				else{
					for(int i = 2; i < V[1].size(); i++)
						ans.push_back(V[1][i]);
					for(int i = 1; i < V[2].size(); i++)
						ans.push_back(V[2][i]);
				}
			}
			if(ans.size() == 0)
				puts("0");
			else{
				sort(ans.begin(),ans.end());
				for(int i = ans.size()-1; i >= 0; i--)
					printf("%d",ans[i]);
				for(int i = 0; i < num; i++)
					printf("0");
				puts("");
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值