Submission #1322109

#TimeUsernameProblemLanguageResultExecution timeMemory
1322109jahongirPotatoes and fertilizers (LMIO19_bulves)C++20
24 / 100
55 ms6512 KiB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(a) a.begin(),a.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef unsigned long long ull;
typedef vector<int> vi;



void solve(){
    int n; cin >> n;
    vector<ll> pref(n+1,0);
    priority_queue<ll> pq;

    ll ans = 0;

    for(int i = 1; i <= n; i++){
        int a,b; cin >> a >> b;
        pref[i] = a-b + pref[i-1];
    }

    for(int i = 1; i <= n; i++){
        if(pref[i] < 0){
            ans += -pref[i];
            pq.push(0);
            pq.pop();
        }else if(pref[i] > pref[n]){
            ans += pref[i] - pref[n];
            pq.push(pref[n]);
            pq.pop();
        }else{
            pq.push(pref[i]);
            if(pq.top() > pref[i]){
                ans += pq.top() - pref[i];
                pq.pop(); pq.push(pref[i]);
            }
        }
    }

    cout << ans;
}

signed main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while(t--){solve();}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...