#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef long double ld;
typedef pair<ld, ld> pld;
typedef pair<lli, lli> pll;
#define mp(x, y) make_pair(x, y)
const lli MAXN = 5e5+5;
lli N;
lli a[MAXN], b[MAXN];
int main() {
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
cin >> N;
for (lli i = 0; i < N; i++) cin >> a[i] >> b[i];
priority_queue<pll> fpos, bpos;
fpos.emplace(-1e9, 0);
bpos.emplace(-1e9, 0);
for (lli i = 0; i < N; i++) {
if (a[i] >= b[i]) {
a[i] -= b[i];
b[i] = 0;
fpos.emplace(-i, a[i]);
} else {
b[i] -= a[i];
a[i] = 0;
}
}
lli ans = 0;
for (lli i = 0; i < N; i++) {
while (-fpos.top().first <= i) {
bpos.emplace(-fpos.top().first, fpos.top().second);
fpos.pop();
}
while (b[i] > 0) {
lli fp = -fpos.top().first, bp = bpos.top().first;
lli fd = fp - i, bd = i - bp;
if (bd < fd) {
lli vl = bpos.top().second;
if (vl <= b[i]) {
ans += bd*vl;
b[i] -= vl;
bpos.pop();
} else {
ans += bd*b[i];
vl -= b[i];
b[i] = 0;
pll tmp = mp(bp, vl);
bpos.pop();
bpos.push(tmp);
}
} else {
lli vl = fpos.top().second;
if (vl <= b[i]) {
ans += fd*vl;
b[i] -= vl;
fpos.pop();
} else {
ans += fd*b[i];
vl -= b[i];
b[i] = 0;
pll tmp = mp(-fp, vl);
fpos.pop();
fpos.push(tmp);
}
}
}
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |