# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1278136 | lmquan | Potatoes and fertilizers (LMIO19_bulves) | C++20 | 103 ms | 10444 KiB |
#define taskname ""
#include <bits/stdc++.h>
using namespace std;
int main() {
if (fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
long long x = 0;
vector<pair<int, int>> a(n + 1);
vector<long long> b(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
x += a[i].first - a[i].second;
b[i] = b[i - 1] + a[i].first - a[i].second;
}
long long result = 0;
for (int i = 1; i <= n; i++) {
if (b[i] < 0) {
result += -b[i];
b[i] = 0;
}
if (b[i] > x) {
result += b[i] - x;
b[i] = x;
}
}
result += x - b[n];
b[n] = x;
priority_queue<int> pq;
for (int i = 1; i <= n; i++) {
pq.push(b[i]);
if (b[i] < pq.top()) {
result += pq.top() - b[i];
pq.pop();
pq.push(b[i]);
}
}
cout << result << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |