#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
ll fst = 0; // value of DP function at 0
priority_queue<ll> points; // points where DP function changes slope
int main() {
cin >> N;
vector<ll> dif(N + 1);
for (int i = 1; i <= N; ++i) {
int a, b;
cin >> a >> b;
dif[i] = a - b + dif[i - 1];
}
assert(dif[N] >= 0); // assume solution exists
for (int i = 1; i < N; ++i) {
if (dif[i] < 0) fst -= dif[i], dif[i] = 0;
fst += dif[i];
points.push(dif[i]);
points.push(dif[i]);
points.pop();
}
while (points.size()) {
ll a = points.top();
points.pop();
fst -= min(a, dif[N]);
}
cout << fst << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
21 ms |
1332 KB |
Output is correct |
5 |
Correct |
46 ms |
2224 KB |
Output is correct |
6 |
Correct |
153 ms |
4408 KB |
Output is correct |
7 |
Correct |
254 ms |
8408 KB |
Output is correct |
8 |
Correct |
267 ms |
13268 KB |
Output is correct |
9 |
Correct |
226 ms |
12604 KB |
Output is correct |
10 |
Correct |
170 ms |
10312 KB |
Output is correct |
11 |
Correct |
176 ms |
10356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
21 ms |
1332 KB |
Output is correct |
5 |
Correct |
46 ms |
2224 KB |
Output is correct |
6 |
Correct |
153 ms |
4408 KB |
Output is correct |
7 |
Correct |
254 ms |
8408 KB |
Output is correct |
8 |
Correct |
267 ms |
13268 KB |
Output is correct |
9 |
Correct |
226 ms |
12604 KB |
Output is correct |
10 |
Correct |
170 ms |
10312 KB |
Output is correct |
11 |
Correct |
176 ms |
10356 KB |
Output is correct |
12 |
Correct |
77 ms |
4076 KB |
Output is correct |
13 |
Correct |
184 ms |
10892 KB |
Output is correct |
14 |
Correct |
256 ms |
15196 KB |
Output is correct |
15 |
Correct |
277 ms |
13412 KB |
Output is correct |
16 |
Correct |
265 ms |
12576 KB |
Output is correct |
17 |
Correct |
162 ms |
10320 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
372 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
21 ms |
1332 KB |
Output is correct |
12 |
Correct |
46 ms |
2224 KB |
Output is correct |
13 |
Correct |
153 ms |
4408 KB |
Output is correct |
14 |
Correct |
254 ms |
8408 KB |
Output is correct |
15 |
Correct |
267 ms |
13268 KB |
Output is correct |
16 |
Correct |
226 ms |
12604 KB |
Output is correct |
17 |
Correct |
170 ms |
10312 KB |
Output is correct |
18 |
Correct |
176 ms |
10356 KB |
Output is correct |
19 |
Correct |
77 ms |
4076 KB |
Output is correct |
20 |
Correct |
184 ms |
10892 KB |
Output is correct |
21 |
Correct |
256 ms |
15196 KB |
Output is correct |
22 |
Correct |
277 ms |
13412 KB |
Output is correct |
23 |
Correct |
265 ms |
12576 KB |
Output is correct |
24 |
Correct |
162 ms |
10320 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
372 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
75 ms |
4072 KB |
Output is correct |
34 |
Correct |
188 ms |
11160 KB |
Output is correct |
35 |
Correct |
315 ms |
15164 KB |
Output is correct |
36 |
Correct |
278 ms |
12756 KB |
Output is correct |
37 |
Correct |
278 ms |
13268 KB |
Output is correct |
38 |
Correct |
309 ms |
15120 KB |
Output is correct |
39 |
Correct |
200 ms |
11324 KB |
Output is correct |
40 |
Correct |
210 ms |
10812 KB |
Output is correct |
41 |
Correct |
172 ms |
10324 KB |
Output is correct |
42 |
Correct |
172 ms |
10320 KB |
Output is correct |
43 |
Correct |
183 ms |
10320 KB |
Output is correct |
44 |
Correct |
171 ms |
10400 KB |
Output is correct |
45 |
Correct |
303 ms |
15188 KB |
Output is correct |
46 |
Correct |
208 ms |
10704 KB |
Output is correct |
47 |
Correct |
183 ms |
10232 KB |
Output is correct |