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 ai represents 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.

Examples
Input

Copy
4
5 0 5 0
Output

Copy
0
Input

Copy
11
5 5 5 5 5 5 5 5 0 5 5
Output

Copy
5555555550
Note

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==0即可;

注意题目是90;简单模拟一下即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int a[maxn];
bool cmp(int a, int b) { return a > b; }
int main() {
//ios::sync_with_stdio(0);
int n; int cnt = 0; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i]; if (a[i] == 0)cnt++;
}
sort(a + 1, a + 1 + n, cmp);
int sum = 0; int tot = 0; bool fg = 0; int maxx = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == 0)break;
sum += a[i]; tot++;
// cout << sum << endl;
if (sum % 9 == 0) {
fg = 1; maxx = max(maxx, tot);
} } if (!fg&&cnt==0)cout << -1 << endl;
else {
if (maxx == 0&&cnt) {
cout << 0 << endl; return 0;
}
if (cnt == 0) {
cout << -1 << endl; return 0;
}
for (int i = 1; i <= maxx; i++)cout << 5;
for (int i = 1; i <= cnt; i++)cout << 0;
} return 0;
}

最新文章

  1. 【JAVA并发编程实战】10、并发程序的测试
  2. 修改Firefox的User-Agent,伪装修改秘籍
  3. AutoCAD学习笔记
  4. php-fpm的配置和优化
  5. Linux svn 回滚版本库
  6. FooTable高级的响应式表格jQuery插件
  7. Objective-C--- 多态 、 协议
  8. 转载-smarty教程(基本语法)
  9. java中File类应用:遍历文件夹下所有文件
  10. Linux(Ubuntu) OpenGL 开发环境
  11. 【转】How to create a new user and grant permissions in MySQL
  12. 移动端适配--flexible.js
  13. 万物云平台数据转发 c# 控制台程序
  14. atime,mtime,ctime 的理解
  15. ESPCN超分辨率汇总
  16. 解决方案:CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\--”--“拒绝访问。 ”
  17. sql server中的工作线程
  18. LeetCode——Maximum Binary Tree
  19. POJ 1741 树分治
  20. redmine 的安装

热门文章

  1. Django 多条件多表查询实例问题
  2. appium运行时启动失败
  3. windows服务编写和“以管理员运行”程序的方法
  4. 数据库最佳实践:DBA小马如何走上升值加薪之路?
  5. javascript——对象的概念——Object(未完)
  6. JavaScript实现重置表单(reset)的方法
  7. android 自定义控件之事件
  8. Codeforces 57C (1-n递增方案数,组合数取模,lucas)
  9. 说说excel
  10. Luogu 4409 [ZJOI2006]皇帝的烦恼