Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i, a, b) for(int i = a; i < b; ++i)
  5. #define REP(i, n) FOR(i, 0, n)
  6. #define _ << " " <<
  7. #define sz(x) ((int) x.size())
  8. #define pb(x) push_back(x)
  9. #define TRACE(x) cerr << #x << " = " << x << endl
  10.  
  11. typedef long long ll;
  12. typedef pair<ll, ll> point;
  13.  
  14. const int MAXN = 5e5 + 5, off = 1 << 20, inf = 1e9 + 5;
  15.  
  16. int n;
  17. multiset <ll> S;
  18. point a[MAXN];
  19.  
  20. int main(){
  21.   ios_base::sync_with_stdio(false); cin.tie(0);
  22.  
  23.   cin >> n;
  24.  
  25.   ll sol = 0;
  26.   REP(i, n)
  27.     cin >> a[i].second >> a[i].first;
  28.  
  29.   for(int i = n - 1; i >= 0; --i){
  30.     if(a[i].second == 1)
  31.       S.insert(a[i].first);
  32.     else{
  33.       ll x = 0;
  34.       if(sz(S)) { auto it = S.end(); it --; x = *it; }
  35.       ll profit = x - a[i].first;
  36.       if(profit > 0){
  37.         sol += profit;
  38.         S.erase(S.find(x));
  39.         S.insert(x - profit);
  40.       }
  41.     }
  42.   }
  43.   cout << sol;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement