Advertisement
tungSfer

Untitled

Jul 16th, 2021
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define el endl
  5. #define umi unordered_map<int, int>
  6. #define umll unordered_map<ll, ll>
  7. #define all(vect) vect.begin(), vect.end()
  8. #define reset(A) memset(A, 0, sizeof(A))
  9. #define approx(n) fixed << setprecision(n)
  10. #define pb push_back
  11.  
  12. const int mod = 1e9 + 7;
  13.  
  14. using namespace std;
  15.  
  16. void solve()
  17. {
  18.     int n, sum = 0;
  19.     cin >> n;
  20.     int a[n];
  21.     for(int i =0; i< n; i++)
  22.     {
  23.         cin >>a[i];
  24.         sum += a[i];
  25.     }
  26.     sort(a, a + n, greater<int>());
  27.     if(!(sum % 3))
  28.     {
  29.         for(int i = 0; i< n; i++)
  30.             cout << a[i];
  31.         cout << el;
  32.         return;
  33.     }
  34.     else if(sum % 3 == 1)
  35.     {
  36.         int ex = -1;
  37.         for(int i = n - 1; i >= 0; i--)
  38.         {
  39.             if(a[i] % 3 == 1)
  40.             {
  41.                 ex = i;
  42.                 break;
  43.             }
  44.         }
  45.         if(ex > -1)
  46.         {
  47.             for(int i = 0; i < n; i++)
  48.             {
  49.                 if(i != ex)
  50.                     cout << a[i];
  51.             }
  52.             cout << el;
  53.             return;
  54.         }
  55.     }
  56.     else if(sum % 3 == 2)
  57.     {
  58.         int ex1 = -1, ex2 = -1, ex = -1;
  59.         for(int i = n - 1; i >= 0; i--)
  60.         {
  61.             if(a[i] % 3 == 2)
  62.             {
  63.                 ex = i;
  64.                 break;
  65.             }
  66.         }
  67.         if(ex > -1)
  68.         {
  69.             for(int i = 0; i < n; i++)
  70.             {
  71.                 if(i != ex)
  72.                     cout << a[i];
  73.             }
  74.             cout << el;
  75.             return;
  76.         }
  77.         for(int i = n - 1; i >= 0; i--)
  78.         {
  79.             if(a[i] % 3 == 1)
  80.             {
  81.                 ex1 = i;
  82.                 break;
  83.             }
  84.         }
  85.         for(int i = ex1 - 1; i >= 0; i--)
  86.         {
  87.             if(a[i] % 3 == 1)
  88.             {
  89.                 ex2 = i;
  90.                 break;
  91.             }
  92.         }
  93.         if(ex1 > -1 && ex2 > -1)
  94.         {
  95.             for(int i = 0; i < n; i++)
  96.             {
  97.                 if(i != ex1 && i != ex2)
  98.                     cout << a[i];
  99.             }
  100.             cout << el;
  101.             return;
  102.         }
  103.     }
  104.     cout << -1 << el;
  105. }
  106.  
  107. int main()
  108. {
  109.     int t = 1;
  110.     cin >> t;
  111. //  cin.ignore();
  112.     while(t--)
  113.     {
  114.         solve();
  115.     }
  116.     return 0;
  117. }
  118.  
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement