CodeForces - 214B

本文介绍了一个编程问题,任务是利用给定的数字集合构造一个尽可能大的整数,并且该整数能够被2、3和5整除。文章详细阐述了解决方案的实现思路和具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值