#include <bits/stdc++.h>
using namespace std;
#define int long long
int N;
int fst = 0; // value of DP function at 0
priority_queue<int> points; // points where DP function changes slope
signed main() {
cin >> N;
vector<int> dif(N+1);
for (int i = 1; i <= N; ++i) {
int a,b; cin >> a >> b;
dif[i] = a-b+dif[i-1];
}
for (int i = 1; i < N; ++i) {
if (dif[i] < 0) fst -= dif[i], dif[i] = 0;
points.push(dif[i]); points.push(dif[i]);
fst += points.top() - dif[i];
points.pop();
}
if(N == 1){
cout << 0 << endl;
return 0;
}
points.push(0);
int mult = 0;
int last = points.top();
while(!points.empty()){
int at = points.top();
//cout << "/ " << at << " " << last << " " << mult << endl;
if(at <= dif[N]){
fst += (last - dif[N]) * mult;
break;
}
fst += (last - at) * mult;
last = at;
mult ++;
points.pop();
}
cout << fst << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
30 ms |
1316 KB |
Output is correct |
5 |
Correct |
62 ms |
2108 KB |
Output is correct |
6 |
Correct |
233 ms |
4412 KB |
Output is correct |
7 |
Correct |
368 ms |
8420 KB |
Output is correct |
8 |
Correct |
396 ms |
8500 KB |
Output is correct |
9 |
Correct |
318 ms |
12664 KB |
Output is correct |
10 |
Correct |
233 ms |
10312 KB |
Output is correct |
11 |
Correct |
246 ms |
10304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
30 ms |
1316 KB |
Output is correct |
5 |
Correct |
62 ms |
2108 KB |
Output is correct |
6 |
Correct |
233 ms |
4412 KB |
Output is correct |
7 |
Correct |
368 ms |
8420 KB |
Output is correct |
8 |
Correct |
396 ms |
8500 KB |
Output is correct |
9 |
Correct |
318 ms |
12664 KB |
Output is correct |
10 |
Correct |
233 ms |
10312 KB |
Output is correct |
11 |
Correct |
246 ms |
10304 KB |
Output is correct |
12 |
Correct |
107 ms |
4064 KB |
Output is correct |
13 |
Correct |
266 ms |
11048 KB |
Output is correct |
14 |
Correct |
388 ms |
15188 KB |
Output is correct |
15 |
Correct |
398 ms |
13388 KB |
Output is correct |
16 |
Correct |
377 ms |
12660 KB |
Output is correct |
17 |
Correct |
216 ms |
10312 KB |
Output is correct |
18 |
Correct |
2 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
296 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
352 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
30 ms |
1316 KB |
Output is correct |
12 |
Correct |
62 ms |
2108 KB |
Output is correct |
13 |
Correct |
233 ms |
4412 KB |
Output is correct |
14 |
Correct |
368 ms |
8420 KB |
Output is correct |
15 |
Correct |
396 ms |
8500 KB |
Output is correct |
16 |
Correct |
318 ms |
12664 KB |
Output is correct |
17 |
Correct |
233 ms |
10312 KB |
Output is correct |
18 |
Correct |
246 ms |
10304 KB |
Output is correct |
19 |
Correct |
107 ms |
4064 KB |
Output is correct |
20 |
Correct |
266 ms |
11048 KB |
Output is correct |
21 |
Correct |
388 ms |
15188 KB |
Output is correct |
22 |
Correct |
398 ms |
13388 KB |
Output is correct |
23 |
Correct |
377 ms |
12660 KB |
Output is correct |
24 |
Correct |
216 ms |
10312 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
3 ms |
332 KB |
Output is correct |
27 |
Correct |
3 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
3 ms |
352 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
3 ms |
300 KB |
Output is correct |
32 |
Correct |
2 ms |
296 KB |
Output is correct |
33 |
Correct |
98 ms |
4068 KB |
Output is correct |
34 |
Correct |
235 ms |
10916 KB |
Output is correct |
35 |
Correct |
407 ms |
15308 KB |
Output is correct |
36 |
Correct |
331 ms |
12724 KB |
Output is correct |
37 |
Correct |
366 ms |
13264 KB |
Output is correct |
38 |
Correct |
417 ms |
15128 KB |
Output is correct |
39 |
Correct |
261 ms |
11340 KB |
Output is correct |
40 |
Correct |
258 ms |
10844 KB |
Output is correct |
41 |
Correct |
233 ms |
10312 KB |
Output is correct |
42 |
Correct |
226 ms |
10296 KB |
Output is correct |
43 |
Correct |
233 ms |
10308 KB |
Output is correct |
44 |
Correct |
225 ms |
10292 KB |
Output is correct |
45 |
Correct |
415 ms |
15304 KB |
Output is correct |
46 |
Correct |
248 ms |
10696 KB |
Output is correct |
47 |
Correct |
229 ms |
10224 KB |
Output is correct |