Ankit_132

C

Oct 30th, 2023
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7.  
  8. int main()
  9. {
  10.     _test
  11.     {
  12.         int n;
  13.         cin>>n;
  14.  
  15.         vector<ll int> a(n);
  16.         for(auto &e: a)     cin>>e;
  17.  
  18.         sort(a.begin(), a.end());
  19.  
  20.         ll int tot = accumulate(a.begin(), a.end(), 0ll);
  21.         ll int ans = tot;
  22.         ll int sum = 0;
  23.         ll int cnt = n;
  24.  
  25.         for(auto e: a)
  26.         {
  27.             sum += e;
  28.             tot -= e;
  29.             cnt--;
  30.  
  31.             if(tot <= sum)
  32.             {
  33.                 if(tot < sum)
  34.                 {
  35.                     ll int x = sum-tot;
  36.                     x /= 2;
  37.  
  38.                     if(x)
  39.                     {
  40.                         cnt++;
  41.                         tot += x;
  42.                         sum -= x;
  43.                     }
  44.                 }
  45.  
  46.                 ans = sum + cnt;
  47.                 break;
  48.             }
  49.         }
  50.  
  51.         cout<<ans<<"\n";
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment