제출 #1135953

#제출 시각아이디문제언어결과실행 시간메모리
1135953MateiKing80Potatoes and fertilizers (LMIO19_bulves)C++20
100 / 100
219 ms12468 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main()
{
    int n;
    cin >> n;

    vector<ll> a(n), b(n);
    for(int i = 0; i < n; i ++)
        cin >> a[i] >> b[i];

    ll c = 0;
    for(int i = 0; i < n; i ++)
        c += a[i] - b[i];

    ll cost = 0;

    priority_queue<ll> pq;
    ll d = 0;
    for(int i = 0; i < n; i ++)
    {
        d += a[i] - b[i];
        ll e = d;
        if(e < 0)
        {
            cost -= e;
            e = 0;
        }
        else if(e > c)
        {
            cost += e - c;
            e = c;
        }
        pq.push(e);
        pq.push(e);
        cost += pq.top() - e;
        pq.pop();
    }
    cout << cost;
}
#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...