Submission #770856

#TimeUsernameProblemLanguageResultExecution timeMemory
770856The_SamuraiPotatoes and fertilizers (LMIO19_bulves)C++17
0 / 100
1077 ms212 KiB
#include "bits/stdc++.h"

using namespace std;
using ll = long long;

void solve() {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    set<int, greater<>> pos1;
    set<int> pos2;
    for (int i = 0; i < n; i++) {
        cin >> a[i].first >> a[i].second;
        if (a[i].first > a[i].second) {
            pos1.insert(i);
            pos2.insert(i);
        }
    }
    int j = 0;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        if (a[i].first >= a[i].second) {
            continue;
        }
        while (a[i].first )
        while (a[i].first < a[i].second) {
            while (j < i and a[j].first <= a[j].second) {
                j++;
            }
            if (j != i) {
                int x = min(a[i].second - a[i].first, a[j].first - a[j].second);
                a[i].first += x;
                a[j].first -= x;
                ans += 1ll * x * abs(j - i);
            } else {
                break;
            }
        }

    }
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);

    int queries = 1;
#ifdef test_cases
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> queries;
#else
    //    cin >> queries;
#endif

    for (int test_case = 1; test_case <= queries; test_case++) {
        solve();
        cout << '\n';
    }
}
#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...