Advertisement
tepyotin2

Untitled

Jul 8th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isDiag = false;
  5. vector<pair<int,int>> listV ;
  6. int main(){
  7.    
  8.     //isDiag = true;
  9.     freopen("pairup.in", "r", stdin);
  10.     int n;
  11.     cin >> n ;
  12.    
  13.     int x, y;
  14.     if(isDiag) cout << "n: " << n  << endl;
  15.     for (int i = 0; i < n; i++)
  16.     {
  17.             cin >> x >> y;
  18.             listV.push_back({y,x});
  19.     }
  20.    
  21.     sort(listV.begin(), listV.end());
  22.     int N = 0;
  23.     int X = n-1;
  24.     long ans = 0;
  25.     while (N <= X)
  26.     {
  27.         long  va =  listV[N].first +  listV[X].first;
  28.         int burn = min(listV[N].second, listV[X].second);  // must optimze burn for batch operation
  29.         listV[N].second = listV[N].second - burn;
  30.         if (listV[N].second == 0)
  31.         {
  32.             N++;
  33.         }
  34.         listV[X].second = listV[X].second - burn;
  35.         if (listV[X].second == 0)
  36.         {
  37.             X--;
  38.         }
  39.    
  40.         ans = max( ans, va);
  41.         if (isDiag) cout << "N: " << N << ", X: " << X  << "burn: " << burn << ", va: " << va << ", ans: " << ans << endl;
  42.     }
  43.    
  44.    
  45.     if (!isDiag) freopen("pairup.out", "w", stdout);
  46.     cout << ans << endl;
  47.    
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement