#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::vector<int64_t> a(N);
for (int i = 0; i < N; ++i) {
int64_t x, y;
std::cin >> x >> y;
a[i] = x - y;
}
std::priority_queue<int64_t> pq;
int64_t sum = 0, cost = 0;
for (int i = 0; i < N - 1; ++i) {
sum += a[i];
int64_t x = sum < 0 ? (cost += -sum, 0) : sum;
pq.push(x);
if (!pq.empty() && x < pq.top()) {
cost += pq.top() - x;
pq.pop();
pq.push(x);
}
}
sum += a[N - 1];
while (!pq.empty() && pq.top() > sum) {
cost += pq.top() - sum;
pq.pop();
}
std::cout << cost << '\n';
exit(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
12 ms |
1616 KB |
Output is correct |
5 |
Correct |
25 ms |
2896 KB |
Output is correct |
6 |
Correct |
87 ms |
7740 KB |
Output is correct |
7 |
Correct |
86 ms |
15204 KB |
Output is correct |
8 |
Correct |
133 ms |
13284 KB |
Output is correct |
9 |
Correct |
95 ms |
12604 KB |
Output is correct |
10 |
Correct |
80 ms |
10340 KB |
Output is correct |
11 |
Correct |
84 ms |
10344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
12 ms |
1616 KB |
Output is correct |
5 |
Correct |
25 ms |
2896 KB |
Output is correct |
6 |
Correct |
87 ms |
7740 KB |
Output is correct |
7 |
Correct |
86 ms |
15204 KB |
Output is correct |
8 |
Correct |
133 ms |
13284 KB |
Output is correct |
9 |
Correct |
95 ms |
12604 KB |
Output is correct |
10 |
Correct |
80 ms |
10340 KB |
Output is correct |
11 |
Correct |
84 ms |
10344 KB |
Output is correct |
12 |
Correct |
41 ms |
4100 KB |
Output is correct |
13 |
Correct |
102 ms |
11056 KB |
Output is correct |
14 |
Correct |
92 ms |
15216 KB |
Output is correct |
15 |
Correct |
131 ms |
13280 KB |
Output is correct |
16 |
Correct |
160 ms |
12656 KB |
Output is correct |
17 |
Correct |
59 ms |
10352 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
2 ms |
328 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
12 ms |
1616 KB |
Output is correct |
12 |
Correct |
25 ms |
2896 KB |
Output is correct |
13 |
Correct |
87 ms |
7740 KB |
Output is correct |
14 |
Correct |
86 ms |
15204 KB |
Output is correct |
15 |
Correct |
133 ms |
13284 KB |
Output is correct |
16 |
Correct |
95 ms |
12604 KB |
Output is correct |
17 |
Correct |
80 ms |
10340 KB |
Output is correct |
18 |
Correct |
84 ms |
10344 KB |
Output is correct |
19 |
Correct |
41 ms |
4100 KB |
Output is correct |
20 |
Correct |
102 ms |
11056 KB |
Output is correct |
21 |
Correct |
92 ms |
15216 KB |
Output is correct |
22 |
Correct |
131 ms |
13280 KB |
Output is correct |
23 |
Correct |
160 ms |
12656 KB |
Output is correct |
24 |
Correct |
59 ms |
10352 KB |
Output is correct |
25 |
Correct |
2 ms |
328 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
316 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
34 ms |
4096 KB |
Output is correct |
34 |
Correct |
77 ms |
10932 KB |
Output is correct |
35 |
Correct |
136 ms |
15164 KB |
Output is correct |
36 |
Correct |
118 ms |
12732 KB |
Output is correct |
37 |
Correct |
106 ms |
13296 KB |
Output is correct |
38 |
Correct |
129 ms |
15340 KB |
Output is correct |
39 |
Correct |
69 ms |
11324 KB |
Output is correct |
40 |
Correct |
78 ms |
10940 KB |
Output is correct |
41 |
Correct |
65 ms |
10404 KB |
Output is correct |
42 |
Correct |
67 ms |
10344 KB |
Output is correct |
43 |
Correct |
68 ms |
10432 KB |
Output is correct |
44 |
Correct |
71 ms |
10352 KB |
Output is correct |
45 |
Correct |
147 ms |
15208 KB |
Output is correct |
46 |
Correct |
65 ms |
10812 KB |
Output is correct |
47 |
Correct |
77 ms |
10252 KB |
Output is correct |