Saleh127

codeforces 558A.cpp

May 23rd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define test int t; cin>>t; while (t--)
  4. #define ll long long int
  5. int main()
  6. {
  7. vector<pair<ll,ll> >a,c;
  8. ll n,x,d,e=0,f,i,j,k,l;
  9.  
  10. cin>>n;
  11. for(i=0;i<n;i++)
  12. {
  13. cin>>x>>d;
  14. if(x>0)
  15. {
  16. a.push_back(make_pair(x,d));
  17. }
  18. else
  19. {
  20. c.push_back(make_pair(x,d));
  21. }
  22. }
  23.  
  24. sort(a.begin(),a.end());
  25. sort(c.begin(),c.end());
  26.  
  27. i=0;
  28. k=a.size();
  29. l=c.size();
  30. j=l-1; ///j for neg after sorting last index has highest value;
  31.  
  32. while(i<k && j>=0)
  33. {
  34. e+=a[i].second + c[j].second;
  35. ++i,--j;
  36. }
  37.  
  38. if(k>l) e+=a[i].second;
  39.  
  40. else if(k<l) e+=c[j].second;
  41.  
  42. cout<<e<<endl;
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment