Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: B. Hometask
- // Contest: Codeforces - Codeforces Round #131 (Div. 2)
- // Memory Limit: 256 MB
- // Time Limit: 2000 ms
- // Date / Time: 2021-08-16 09:24:28
- // Author: cosenza
- // всё что ни делается - всё к лучшему
- // check list -> long long, special cases, array size, mod (a*b%p*c%p not a*b*c%p , (a-b+p)%p not a-b )
- //
- // Powered by CP Editor (https://cpeditor.org)
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int n;
- cin >> n;
- multiset<int> ms;
- int cnt0 = 0, r1 = 0, r2 = 0;
- for(int i = 0; i < n; i++) {
- int k;
- cin >> k;
- ms.insert(k);
- if(k == 0) {
- cnt0++;
- }
- if(k % 3 == 1) {
- r1++;
- } else if(k % 3 == 2) {
- r2++;
- }
- }
- int tot = accumulate(ms.begin(), ms.end(), 0);
- if(!cnt0) {
- cout << "-1\n";
- return 0;
- }
- if(tot % 3 == 1) {
- if(r1) {
- for(auto k : ms) {
- if(k % 3 == 1) {
- ms.erase(ms.find(k));
- break;
- }
- }
- } else if(r2) {
- int cnt = 0;
- for(auto k : ms) {
- if(k % 3 == 2 and cnt < 2) {
- ms.erase(ms.find(k));
- cnt++;
- }
- if(cnt == 2) {
- break;
- }
- }
- } else {
- cout << "-1\n";
- return 0;
- }
- } else if(tot % 3 == 2) {
- if(r2) {
- for(auto k : ms) {
- if(k % 3 == 2) {
- ms.erase(ms.find(k));
- break;
- }
- }
- } else if(r1) {
- int cnt = 0;
- for(auto k : ms) {
- if(k % 3 == 1 and cnt < 2) {
- ms.erase(ms.find(k));
- cnt++;
- }
- if(cnt == 2) {
- break;
- }
- }
- } else {
- cout << "-1\n";
- return 0;
- }
- }
- vector<int> v(ms.begin(), ms.end());
- reverse(v.begin(), v.end());
- for(auto k : v) {
- cout << k;
- }
- cout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement