Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define el endl
- #define umi unordered_map<int, int>
- #define umll unordered_map<ll, ll>
- #define all(vect) vect.begin(), vect.end()
- #define reset(A) memset(A, 0, sizeof(A))
- #define approx(n) fixed << setprecision(n)
- #define pb push_back
- const int mod = 1e9 + 7;
- using namespace std;
- void solve()
- {
- int n, sum = 0;
- cin >> n;
- int a[n];
- for(int i =0; i< n; i++)
- {
- cin >>a[i];
- sum += a[i];
- }
- sort(a, a + n, greater<int>());
- if(!(sum % 3))
- {
- for(int i = 0; i< n; i++)
- cout << a[i];
- cout << el;
- return;
- }
- else if(sum % 3 == 1)
- {
- int ex = -1;
- for(int i = n - 1; i >= 0; i--)
- {
- if(a[i] % 3 == 1)
- {
- ex = i;
- break;
- }
- }
- if(ex > -1)
- {
- for(int i = 0; i < n; i++)
- {
- if(i != ex)
- cout << a[i];
- }
- cout << el;
- return;
- }
- }
- else if(sum % 3 == 2)
- {
- int ex1 = -1, ex2 = -1, ex = -1;
- for(int i = n - 1; i >= 0; i--)
- {
- if(a[i] % 3 == 2)
- {
- ex = i;
- break;
- }
- }
- if(ex > -1)
- {
- for(int i = 0; i < n; i++)
- {
- if(i != ex)
- cout << a[i];
- }
- cout << el;
- return;
- }
- for(int i = n - 1; i >= 0; i--)
- {
- if(a[i] % 3 == 1)
- {
- ex1 = i;
- break;
- }
- }
- for(int i = ex1 - 1; i >= 0; i--)
- {
- if(a[i] % 3 == 1)
- {
- ex2 = i;
- break;
- }
- }
- if(ex1 > -1 && ex2 > -1)
- {
- for(int i = 0; i < n; i++)
- {
- if(i != ex1 && i != ex2)
- cout << a[i];
- }
- cout << el;
- return;
- }
- }
- cout << -1 << el;
- }
- int main()
- {
- int t = 1;
- cin >> t;
- // cin.ignore();
- while(t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement