Codeforces Round #204 (Div. 2)——A找规律——Jeff and Digits

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 a1a2, ..., 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个5能被9整除
   把0全部放到最后,如果没有0就失败
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int a[1100];
    while(~scanf("%d", &n)){
        for(int i = 1; i <= n ;i++)
            scanf("%d", &a[i]);
        int num1 = 0, num2 = 0;
        for(int i = 1; i <= n ;i++){
            if(a[i] == 5)
                num1++;
            if(a[i] == 0)
                num2++;
        }
        num1/= 9;
             if(num2 == 0 ){ printf("-1\n");continue;}
        if(num1 == 0 && num2 != 0){ printf("0\n");continue;}
        if(num1 == 0 && num2 == 0){printf("-1\n"); continue;}
        else {
        for(int i = 1; i <= num1; i++){
            printf("555555555");
        }
        for(int i = 1; i <= num2; i++)
         printf("0");
        }
        puts("");
    }
    return 0;
}

  

时间: 2024-10-14 18:51:27

Codeforces Round #204 (Div. 2)——A找规律——Jeff and Digits的相关文章

Codeforces Round #242 (Div. 2)C(找规律,异或运算)

一看就是找规律的题.只要熟悉异或的性质,可以秒杀. 为了防止忘记异或的规则,可以把异或理解为半加运算:其运算法则相当于不带进位的二进制加法. 一些性质如下: 交换律: 结合律: 恒等律: 归零律: 典型应用:交换a和b的值:a=a^b^(b=a); #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<

CF&amp;&amp;CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的数都下取整,累积更改的sum 那么选1个小数上取整,就会使sum-1 整数上下取整不会产生影响 所以有1个整数就可以使上取整的小数少1个 所以ans=min(abs(i-sum)) i∈[n-整数个数,n] #include<cmath> #include<cstdio> #inclu

CF&amp;&amp;CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 然后可以对区间剩余数重排 继续删除下标为等差数列且数值相等的一些数 继续对区间进行重排 直至区间内没有数 问每次操作的最少删除次数 因为每次删除后可以重排 那么完全可以在第一次删除后就把区间相等的数排在一起 这样相等的数的下标就分别构成了公差为1的等差数列 所以问题转化为 设区间[l,r]内数有x

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks

题目传送门 1 /* 2 找规律,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e4 + 10; 12 const int INF = 0x3f3f3f3f; 13 char s

数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

题目传送门 1 /* 2 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <cmath> 9 #include <vector> 10 using namespace std; 11

【排序】【规律】Codeforces Round #254 (Div. 2) - D. Rooter&#39;s Song

D. DZY Loves FFT Source http://codeforces.com/contest/445/problem/D Description Wherever the destination is, whoever we meet, let's render this song together. On a Cartesian coordinate plane lies a rectangular stage of size w?×?h, represented by a re

Codeforces Round #260 (Div. 2)

A. Laptops 题目意思: 给定n台电脑,第i台电脑的价格是ai ,质量是bi ,问是否存在一台电脑价格比某台电脑价格底,但质量确比某台电脑的质量高,即是否存在ai < aj 且 bi > bj ? 解题思路: 这题一定要看题目,a都是1~n的不同数,b也是1~n的不同数,此题只需要判断ai 是否等于bi ,如果ai != bi 的话,则输出“Happy Alex”,如果所有的ai  == bi 则输出“Poor Alex” 证明:先将a按照从小到大排序,当i<j时ai <

Codeforces Round #256 (Div. 2)D 二分答案

D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the multiplication tabl