AlenAntonelli

Paired Up (vec)

Jun 10th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #define ll long long
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     ll n, V=0;
  10.     cin>>n;
  11.     vector< pair<ll, ll> > v;
  12.    
  13.     ll x, y;
  14.     for(ll i=0; i<n; i++)
  15.     {
  16.         cin>>x>>y;
  17.         v.push_back( {y,x} );
  18.         V+=x;
  19.     }
  20.    
  21.     sort(v.begin(), v.end());
  22.    
  23.     /**for(ll i=0; i<v.size(); i++)
  24.         cout<<v[i].first<<"-"<<v[i].second<<" ";
  25.     cout<<endl;**/
  26.    
  27.     ll ini=0;
  28.     ll fin=v.size()-1;
  29.    
  30.     ll suma, may=(-1);
  31.    
  32.     for(ll i=0; i<V/2; i++)
  33.     {
  34.         /*if (ini<0 || ini >=v.size())
  35.             cout<<"ERROR";
  36.         if (fin<0 || fin >=v.size())
  37.             cout<<"ERROR";*/
  38.            
  39.         suma = v[ini].first + v[fin].first;
  40.         if (suma > may)
  41.             may = suma;
  42.        
  43.         v[ini].second--;
  44.         v[fin].second--;
  45.        
  46.        
  47.         if (v[ini].second == 0)
  48.             ini++;
  49.        
  50.         if (v[fin].second == 0)
  51.             fin--;
  52.     }
  53.     cout<<may;
  54.    
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment