-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path214 B Hometask.cpp
125 lines (110 loc) · 3.57 KB
/
214 B Hometask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*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 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.*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int sum=0; int cnt0=0;
vector<int>a(n);
for(int i=0;i<n;i++)
{
cin>>a[i]; sum+=a[i];
if(a[i]==0)
cnt0++;
}
sort(a.begin(),a.end());
reverse(a.begin(),a.end());
if(cnt0==0)
cout<<-1;
else if(sum==0 || a[0]==0 )
cout<<0;
else
{
if(sum%3==0)
{
for(int i=0;i<n;i++)
cout<<a[i];
}
else if(sum%3==1)
{
for(int i=n-1;i>=0;i--)
{
if(a[i]!=0 && a[i]%3==1)
{
sum=sum-a[i];
a[i]=-1;
break;
}
}
if (sum % 3 != 0) {
int test = 0;
for (int i = n - 1; i >= 0; i--) {
if (a[i] != 0 && a[i] % 3 == 2) {
a[i] = -1;
test++;
}
if (test == 2) {
break;
}
}
}
vector<int>ans;
for(int i=0;i<n;i++)
{
if(a[i]!=-1)
ans.push_back(a[i]);
}
if(ans[0]==0)
cout<<0;
else
{
for(int i=0;i<ans.size();i++)
cout<<ans[i];
}
}
else if(sum%3==2)
{
for(int i=n-1;i>=0;i--)
{
if(a[i]!=0 && a[i]%3==2)
{
sum=sum-a[i];
a[i]=-1;
break;
}
}
if (sum % 3 != 0) {
int test = 0;
for (int i = n - 1; i >= 0; i--) {
if (a[i] != 0 && a[i] % 3 == 1) {
a[i] = -1;
test++;
}
if (test == 2) {
break;
}
}
}
vector<int>ans;
for(int i=0;i<n;i++)
{
if(a[i]!=-1)
ans.push_back(a[i]);
}
if(ans[0]==0)
cout<<0;
else
{
for(int i=0;i<ans.size();i++)
cout<<ans[i];
}
}
}
}