Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define FOR(i, a, b) for(int i = a; i < b; ++i)
- #define REP(i, n) FOR(i, 0, n)
- #define _ << " " <<
- #define sz(x) ((int) x.size())
- #define pb(x) push_back(x)
- #define TRACE(x) cerr << #x << " = " << x << endl
- typedef long long ll;
- typedef pair<ll, ll> point;
- const int MAXN = 5e5 + 5, off = 1 << 20, inf = 1e9 + 5;
- int n;
- multiset <ll> S;
- point a[MAXN];
- int main(){
- ios_base::sync_with_stdio(false); cin.tie(0);
- cin >> n;
- ll sol = 0;
- REP(i, n)
- cin >> a[i].second >> a[i].first;
- for(int i = n - 1; i >= 0; --i){
- if(a[i].second == 1)
- S.insert(a[i].first);
- else{
- ll x = 0;
- if(sz(S)) { auto it = S.end(); it --; x = *it; }
- ll profit = x - a[i].first;
- if(profit > 0){
- sol += profit;
- S.erase(S.find(x));
- S.insert(x - profit);
- }
- }
- }
- cout << sol;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement