Sreekar_0125

Untitled

Mar 16th, 2022
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. int main() {
  8.     int t;
  9.     cin>>t;
  10.     while(t--) {
  11.         long n,p=0,q=0;
  12.         long sum1=0,sum2=0,sum3;
  13.         vector<long> v1,v2;
  14.         long x;
  15.         for(long i=0;i<n;i++) {
  16.             cin>>x;
  17.             if(x>0) {
  18.                 v1.push_back(x);
  19.                 p++;
  20.             }
  21.             else {
  22.                 v2.push_back(x);
  23.                 q++;
  24.             }
  25.         }
  26.         sort(v1.begin(),v1.end());
  27.         sort(v2.begin(),v2.end());
  28.        
  29.         while(p>=0 && q>=0)  {
  30.            
  31.         v1.pop_back();
  32.          p--;
  33.            
  34.            
  35.         v2.pop_back();
  36.          q--;
  37.            
  38.         }
  39.               if(p<0) {
  40.                   p = 0;
  41.               }
  42.               if(q<0) {
  43.                   q = 0;
  44.               }
  45.         for(long i=0;i<p;i++) {
  46.             sum1+=v1[i];
  47.         }
  48.         for(long i=0;i<q;i++) {
  49.             sum2+=v2[i];
  50.         }
  51.         sum3 = sum1 + sum2;
  52.         cout<<sum3<<endl;
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment