Submission #1166829

#TimeUsernameProblemLanguageResultExecution timeMemory
1166829tamytePotatoes and fertilizers (LMIO19_bulves)C++20
24 / 100
58 ms6472 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); stack<pair<int, int>> left, right; ll res = 0; for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; int need = min(a[i], b[i]); a[i] -= need; b[i] -= need; } for (int i = n - 1; i >= 0; --i) { if (a[i] > 0) { right.push({a[i], i}); } } for (int i = 0; i < n; ++i) { while (right.size() && right.top().second <= i) { left.push({right.top()}); right.pop(); } while (b[i] > 0) { if (left.size()) { int need = min(b[i], left.top().first); b[i] -= need; left.top().first -= need; int d = abs(i - left.top().second); res += 1LL * d * need; } else { int need = min(b[i], right.top().first); b[i] -= need; right.top().first -= need; int d = abs(i - right.top().second); res += 1LL * d * need; } while (left.size() && left.top().first == 0) left.pop(); while (right.size() && right.top().first == 0) right.pop(); } } // cout << "CNT: " << cnt << endl; cout << res << endl; }
#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...