#include <bits/stdc++.h>
using namespace std;
#define f1r(i, a, b) for (int i = a; i < b; i++)
#define f0r(i, a) f1r(i, 0, a)
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define sz(x) (int) (x).size()
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
vl diff(n+1);
f1r(i, 1, n+1) {
int a, b; cin >> a >> b;
diff[i] = a-b + diff[i-1];
}
ll lst = 0;
multiset<ll> pq;
f1r(i, 1, n) {
if (diff[i] < 0) {
lst -= diff[i];
diff[i] = 0;
}
if (diff[i] > diff[n]) {
lst += diff[i]-diff[n];
diff[i] = diff[n];
}
lst -= diff[i];
f0r(j, 2) pq.insert(diff[i]);
ll val = *pq.rbegin();
lst += val;
pq.erase(prev(pq.end()));
}
if (sz(pq) == 0) {
cout << lst << '\n';
return 0;
}
ll cur = *pq.rbegin();
ll slope = 0;
ll bar = diff[n];
pq.erase(prev(pq.end()));
while (sz(pq) && cur >= bar) {
ll nxt = *pq.rbegin();
pq.erase(prev(pq.end()));
if (nxt <= bar) {
lst += (cur - bar) * -slope;
cout << lst << '\n';
return 0;
} else {
lst += (cur - nxt) * -slope;
slope--;
}
}
if (cur <= bar) {
cout << lst << '\n';
return 0;
}
lst += (cur - bar) * -slope;
cout << lst << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
23 ms |
3436 KB |
Output is correct |
5 |
Correct |
50 ms |
6508 KB |
Output is correct |
6 |
Correct |
136 ms |
17388 KB |
Output is correct |
7 |
Correct |
299 ms |
34540 KB |
Output is correct |
8 |
Correct |
279 ms |
32580 KB |
Output is correct |
9 |
Correct |
271 ms |
31980 KB |
Output is correct |
10 |
Correct |
259 ms |
29740 KB |
Output is correct |
11 |
Correct |
252 ms |
29676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
23 ms |
3436 KB |
Output is correct |
5 |
Correct |
50 ms |
6508 KB |
Output is correct |
6 |
Correct |
136 ms |
17388 KB |
Output is correct |
7 |
Correct |
299 ms |
34540 KB |
Output is correct |
8 |
Correct |
279 ms |
32580 KB |
Output is correct |
9 |
Correct |
271 ms |
31980 KB |
Output is correct |
10 |
Correct |
259 ms |
29740 KB |
Output is correct |
11 |
Correct |
252 ms |
29676 KB |
Output is correct |
12 |
Correct |
60 ms |
8812 KB |
Output is correct |
13 |
Correct |
157 ms |
20844 KB |
Output is correct |
14 |
Correct |
269 ms |
34540 KB |
Output is correct |
15 |
Correct |
272 ms |
32748 KB |
Output is correct |
16 |
Correct |
242 ms |
32108 KB |
Output is correct |
17 |
Correct |
202 ms |
29676 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
396 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
396 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
396 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
23 ms |
3436 KB |
Output is correct |
12 |
Correct |
50 ms |
6508 KB |
Output is correct |
13 |
Correct |
136 ms |
17388 KB |
Output is correct |
14 |
Correct |
299 ms |
34540 KB |
Output is correct |
15 |
Correct |
279 ms |
32580 KB |
Output is correct |
16 |
Correct |
271 ms |
31980 KB |
Output is correct |
17 |
Correct |
259 ms |
29740 KB |
Output is correct |
18 |
Correct |
252 ms |
29676 KB |
Output is correct |
19 |
Correct |
60 ms |
8812 KB |
Output is correct |
20 |
Correct |
157 ms |
20844 KB |
Output is correct |
21 |
Correct |
269 ms |
34540 KB |
Output is correct |
22 |
Correct |
272 ms |
32748 KB |
Output is correct |
23 |
Correct |
242 ms |
32108 KB |
Output is correct |
24 |
Correct |
202 ms |
29676 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
2 ms |
492 KB |
Output is correct |
28 |
Correct |
2 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
31 |
Correct |
2 ms |
492 KB |
Output is correct |
32 |
Correct |
63 ms |
8812 KB |
Output is correct |
33 |
Correct |
163 ms |
20844 KB |
Output is correct |
34 |
Correct |
315 ms |
34540 KB |
Output is correct |
35 |
Correct |
407 ms |
32108 KB |
Output is correct |
36 |
Correct |
277 ms |
32748 KB |
Output is correct |
37 |
Correct |
289 ms |
34540 KB |
Output is correct |
38 |
Correct |
272 ms |
30700 KB |
Output is correct |
39 |
Correct |
201 ms |
28084 KB |
Output is correct |
40 |
Correct |
247 ms |
29676 KB |
Output is correct |
41 |
Correct |
239 ms |
29804 KB |
Output is correct |
42 |
Correct |
237 ms |
29676 KB |
Output is correct |
43 |
Correct |
217 ms |
29676 KB |
Output is correct |
44 |
Correct |
288 ms |
34540 KB |
Output is correct |
45 |
Correct |
262 ms |
30060 KB |
Output is correct |
46 |
Correct |
196 ms |
24428 KB |
Output is correct |
47 |
Correct |
2 ms |
492 KB |
Output is correct |