codeforces 214B Hometask

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



题意:给n个数,求出用这n个数组成的最大的数,要求该数能被2,3,5整除,可以不用完所有的数,但不能有前导0

首先考虑被2,3,5整除数的特征,末位肯定是0,所以数字和能整除3,n个数的和对3取模只会有三种情况:

0 直接输出所有数,但是要注意有多个0时只输出一个

1 要想使剩下的数字和能整除3,要么去掉一个1(模3得1的 数),要么去掉2个2(模3得2的数),当然优先考虑去掉1个1的情况,如果都不满足直接输出3,6,9,0对应的个数,如果只剩下0要注意只输出一个0

2 和上面的一样,要么去掉1个2,要么去掉2个1,剩下和上面一样

代码较挫



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)

const int inf=0x3f3f3f3f;
const int Max=1e5+1;
int a[1100],m,n,b[110],d;

int main()
{
    while(~sf("%d",&m))
    {
        mem(a,0);
        int tag=0;
        int sum=0;
        for1(i,m)
            sf("%d",&d),a[d]++,sum+=d;//完全不需要排序,开个大小为10的数组计数即可
        if(!a[0])
        {
            pf("-1\n");
            continue;
        }
        b[1]=a[1]+a[4]+a[7];
        b[2]=a[2]+a[5]+a[8];
        int p=sum%3;
        if(p==1)//余数为1的情况
        {
            int x=2;
            if(b[1]>=1)
                for(int i=0;i<3;i++)
                {
                    if(a[1+i*3])
                    {
                        a[1+i*3]--;
                        break;
                    }
                }
            else if(b[2]>=2)
            {
                for(int i=0;i<3;i++)
                    while(a[2+3*i])
                    {
                        if(!x)
                            break;
                        a[2+3*i]--;
                        x--;
                    }
            }
            else//两种都不满足,直接将不是3倍数的计数器归0
            {
                for(int i=1;i<=9;i++)
                    if(i%3)
                        a[i]=0;
            }
        }
        else if(p==2)
        {
            int x=2;
            if(b[2]>=1)
                for(int i=0;i<3;i++)
                {
                    if(a[2+i*3])
                    {
                        a[2+i*3]--;
                        break;
                    }
                }
            else if(b[1]>=2)
                for(int i=0;i<3;i++)
                    while(a[1+3*i])
                    {
                        if(!x)break;
                        a[1+3*i]--;
                        x--;
                    }
            else
            {
                for(int i=1;i<=9;i++)
                    if(i%3)
                        a[i]=0;
            }
        }
        int q=0;
        for(int i=1;i<=9;i++)
        {
            q+=a[i];
        }
        if(!q)//判断是不是只剩下0
        {
            pf("0\n");
            continue;
        }
        for(int i=9;i>=0;i--)//从大到小输出
            while(a[i]--)
                pf("%d",i);
        pf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值