#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define int long long
const int MAXN = 5e5;
const ll inf = 1e18;
int n;
int vals[MAXN];
ll pre[MAXN];
int ben[MAXN];
class stree {
public:
int lp, rp;
stree *l = nullptr;
stree *r = nullptr;
pii mx;
int lz = 0;
stree(int lv, int rv) {
lp = lv;
rp = rv;
if (lp < rp) {
int m = (lp+rp)/2;
l = new stree(lp, m);
r = new stree(m+1, rp);
mx = max(l->mx, r->mx);
}
else {
mx = pii(ben[lp], lp);
}
}
void add_to(int v) {
lz += v;
mx.first += v;
}
void push() {
if (l) {
l->add_to(lz);
r->add_to(lz);
}
lz = 0;
}
void upd(int lv, int rv, int v) {
if (lp > rv || rp < lv) return;
if (lp >= lv && rp <= rv) {
return add_to(v);
}
push();
l->upd(lv, rv, v);
r->upd(lv, rv, v);
mx = max(l->mx, r->mx);
}
pii get_mx(int lv, int rv) {
if (lp > rv || rp < lv) return pii(-1, -1);
if (lp >= lv && rp <= rv) return mx;
push();
return max(l->get_mx(lv, rv), r->get_mx(lv, rv));
}
};
stree *mx_tree;
class stree2 {
public:
int lp, rp;
stree2 *l = nullptr;
stree2 *r = nullptr;
ll mn;
ll sub = 0;
stree2(int lv, int rv) {
lp = lv;
rp = rv;
if (lp < rp) {
int m = (lp+rp)/2;
l = new stree2(lp, m);
r = new stree2(m+1, rp);
mn = min(l->mn, r->mn);
}
else {
if (pre[lp] <= 0) mn = inf;
else mn = pre[lp];
}
}
void erase() {
if (mn) return;
if (lp == rp) {
mn = inf;
mx_tree->upd(0, lp, -2);
return;
}
push();
l->erase();
r->erase();
mn = min(l->mn, r->mn);
}
void add_to(int v) {
mn += v;
sub += v;
if (mn == 0) {
erase();
}
}
void push() {
if (l) {
l->add_to(sub);
r->add_to(sub);
}
sub = 0;
}
void upd(int lv, int rv, int v) {
if (lp > rv || rp < lv) return;
if (lp >= lv && rp <= rv) {
add_to(v);
return;
}
push();
l->upd(lv, rv, v);
r->upd(lv, rv, v);
mn = min(l->mn, r->mn);
}
ll query(int lv, int rv) {
if (lp > rv || rp < lv) return inf;
if (lp >= lv && rp <= rv) return mn;
push();
return min(l->query(lv, rv), r->query(lv, rv));
}
};
stree2 *beat_tree;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n;
ll ans = 0;
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
vals[i] = x-y;
pre[i] += vals[i];
if (i < n-1) pre[i+1] = pre[i];
ans += (pre[i] < 0) ? (-pre[i]) : pre[i];
}
for (int i = n-1; i >= 0; i--) {
if (pre[i] <= 0) ben[i]--;
else ben[i]++;
if (i) ben[i-1] = ben[i];
}
mx_tree = new stree(0, n-1);
beat_tree = new stree2(0, n-1);
pii bst = mx_tree->get_mx(0, n-1);
ll rem = pre[n-1];
while (bst.first && rem) {
ll lim = beat_tree->query(bst.second, n-1);
lim = min(lim, rem);
ans -= lim*bst.first;
beat_tree->upd(bst.second, n-1, -lim);
bst = mx_tree->get_mx(0, n-1);
rem -= lim;
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
19 ms |
14004 KB |
Output is correct |
5 |
Correct |
30 ms |
27732 KB |
Output is correct |
6 |
Correct |
82 ms |
68776 KB |
Output is correct |
7 |
Correct |
171 ms |
137208 KB |
Output is correct |
8 |
Correct |
177 ms |
137348 KB |
Output is correct |
9 |
Correct |
152 ms |
137248 KB |
Output is correct |
10 |
Correct |
151 ms |
137220 KB |
Output is correct |
11 |
Correct |
155 ms |
137280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
19 ms |
14004 KB |
Output is correct |
5 |
Correct |
30 ms |
27732 KB |
Output is correct |
6 |
Correct |
82 ms |
68776 KB |
Output is correct |
7 |
Correct |
171 ms |
137208 KB |
Output is correct |
8 |
Correct |
177 ms |
137348 KB |
Output is correct |
9 |
Correct |
152 ms |
137248 KB |
Output is correct |
10 |
Correct |
151 ms |
137220 KB |
Output is correct |
11 |
Correct |
155 ms |
137280 KB |
Output is correct |
12 |
Correct |
45 ms |
34508 KB |
Output is correct |
13 |
Correct |
101 ms |
82440 KB |
Output is correct |
14 |
Correct |
172 ms |
137276 KB |
Output is correct |
15 |
Correct |
162 ms |
137276 KB |
Output is correct |
16 |
Correct |
194 ms |
137292 KB |
Output is correct |
17 |
Correct |
165 ms |
137248 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
2 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
3 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
3 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
3 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
19 ms |
14004 KB |
Output is correct |
12 |
Correct |
30 ms |
27732 KB |
Output is correct |
13 |
Correct |
82 ms |
68776 KB |
Output is correct |
14 |
Correct |
171 ms |
137208 KB |
Output is correct |
15 |
Correct |
177 ms |
137348 KB |
Output is correct |
16 |
Correct |
152 ms |
137248 KB |
Output is correct |
17 |
Correct |
151 ms |
137220 KB |
Output is correct |
18 |
Correct |
155 ms |
137280 KB |
Output is correct |
19 |
Correct |
45 ms |
34508 KB |
Output is correct |
20 |
Correct |
101 ms |
82440 KB |
Output is correct |
21 |
Correct |
172 ms |
137276 KB |
Output is correct |
22 |
Correct |
162 ms |
137276 KB |
Output is correct |
23 |
Correct |
194 ms |
137292 KB |
Output is correct |
24 |
Correct |
165 ms |
137248 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
1 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
93 ms |
34584 KB |
Output is correct |
34 |
Correct |
217 ms |
82512 KB |
Output is correct |
35 |
Correct |
373 ms |
137176 KB |
Output is correct |
36 |
Correct |
487 ms |
137292 KB |
Output is correct |
37 |
Correct |
263 ms |
137424 KB |
Output is correct |
38 |
Correct |
455 ms |
144052 KB |
Output is correct |
39 |
Correct |
266 ms |
140336 KB |
Output is correct |
40 |
Correct |
283 ms |
127144 KB |
Output is correct |
41 |
Correct |
227 ms |
139248 KB |
Output is correct |
42 |
Correct |
202 ms |
139316 KB |
Output is correct |
43 |
Correct |
171 ms |
139340 KB |
Output is correct |
44 |
Correct |
171 ms |
139244 KB |
Output is correct |
45 |
Correct |
267 ms |
143936 KB |
Output is correct |
46 |
Correct |
271 ms |
139668 KB |
Output is correct |
47 |
Correct |
256 ms |
109972 KB |
Output is correct |