# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1265527 | nhphuc | Potatoes and fertilizers (LMIO19_bulves) | C++20 | 109 ms | 16196 KiB |
#include <bits/stdc++.h>
using namespace std;
const int N = 500500;
const long long inf = 1e18;
int n, a[N], b[N];
long long dif[N], c[N], ans = 0;
priority_queue<long long> pq;
int32_t main (){
ios::sync_with_stdio(false); cin.tie(nullptr);
const string task = "test";
if (fopen ((task + ".inp").c_str(), "r")){
freopen ((task + ".inp").c_str(), "r", stdin);
freopen ((task + ".out").c_str(), "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; ++i){
cin >> a[i] >> b[i];
dif[i] = dif[i - 1] + 1ll * (a[i] - b[i]);
}
for (int i = 1; i <= n; ++i){
pq.push(dif[i]);
pq.push(dif[i]);
c[i] = pq.top();
pq.pop();
}
for (int i = n; i >= 1; --i){
c[i] = max(0ll, min({dif[n], c[i], (i < n ? c[i + 1] : inf)}));
ans += abs(dif[i] - c[i]);
}
cout << ans << "\n";
}
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... |