#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5, INF = 1e18 + 7;
int n, a[N], b[N], d[N], pre[N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i] >> b[i], d[i] = a[i] - b[i], pre[i] = pre[i - 1] + d[i];
priority_queue<int> pq;
int ans = 0;
for(int i = 1; i <= n; i++){
if(pre[i] < 0) ans -= pre[i], pre[i] = 0;
pq.push(pre[i]); pq.push(pre[i]);
ans += pq.top() - pre[i];
pq.pop();
}
int x = pq.top(), slope = 1; pq.pop();
while(pq.size()){
int u = pq.top(); pq.pop();
if(u <= pre[n]){
ans += slope * (x - pre[n]); break;
} else{
ans += slope * (x - u);
}
x = u;
slope++;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2780 KB |
Output is correct |
4 |
Correct |
10 ms |
4452 KB |
Output is correct |
5 |
Correct |
22 ms |
6752 KB |
Output is correct |
6 |
Correct |
71 ms |
15056 KB |
Output is correct |
7 |
Correct |
83 ms |
27060 KB |
Output is correct |
8 |
Correct |
120 ms |
25036 KB |
Output is correct |
9 |
Correct |
84 ms |
24380 KB |
Output is correct |
10 |
Correct |
75 ms |
22216 KB |
Output is correct |
11 |
Correct |
80 ms |
22220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2780 KB |
Output is correct |
4 |
Correct |
10 ms |
4452 KB |
Output is correct |
5 |
Correct |
22 ms |
6752 KB |
Output is correct |
6 |
Correct |
71 ms |
15056 KB |
Output is correct |
7 |
Correct |
83 ms |
27060 KB |
Output is correct |
8 |
Correct |
120 ms |
25036 KB |
Output is correct |
9 |
Correct |
84 ms |
24380 KB |
Output is correct |
10 |
Correct |
75 ms |
22216 KB |
Output is correct |
11 |
Correct |
80 ms |
22220 KB |
Output is correct |
12 |
Correct |
32 ms |
8400 KB |
Output is correct |
13 |
Correct |
100 ms |
19276 KB |
Output is correct |
14 |
Correct |
86 ms |
27024 KB |
Output is correct |
15 |
Correct |
122 ms |
25036 KB |
Output is correct |
16 |
Correct |
134 ms |
24528 KB |
Output is correct |
17 |
Correct |
62 ms |
22220 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2452 KB |
Output is correct |
7 |
Correct |
1 ms |
2476 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2780 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2452 KB |
Output is correct |
7 |
Correct |
1 ms |
2476 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2612 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2608 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2780 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2452 KB |
Output is correct |
7 |
Correct |
1 ms |
2476 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2612 KB |
Output is correct |
11 |
Correct |
10 ms |
4452 KB |
Output is correct |
12 |
Correct |
22 ms |
6752 KB |
Output is correct |
13 |
Correct |
71 ms |
15056 KB |
Output is correct |
14 |
Correct |
83 ms |
27060 KB |
Output is correct |
15 |
Correct |
120 ms |
25036 KB |
Output is correct |
16 |
Correct |
84 ms |
24380 KB |
Output is correct |
17 |
Correct |
75 ms |
22216 KB |
Output is correct |
18 |
Correct |
80 ms |
22220 KB |
Output is correct |
19 |
Correct |
32 ms |
8400 KB |
Output is correct |
20 |
Correct |
100 ms |
19276 KB |
Output is correct |
21 |
Correct |
86 ms |
27024 KB |
Output is correct |
22 |
Correct |
122 ms |
25036 KB |
Output is correct |
23 |
Correct |
134 ms |
24528 KB |
Output is correct |
24 |
Correct |
62 ms |
22220 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2608 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
26 ms |
8412 KB |
Output is correct |
34 |
Correct |
61 ms |
19396 KB |
Output is correct |
35 |
Correct |
107 ms |
26840 KB |
Output is correct |
36 |
Correct |
95 ms |
24580 KB |
Output is correct |
37 |
Correct |
105 ms |
25036 KB |
Output is correct |
38 |
Correct |
100 ms |
26924 KB |
Output is correct |
39 |
Correct |
65 ms |
23244 KB |
Output is correct |
40 |
Correct |
74 ms |
22036 KB |
Output is correct |
41 |
Correct |
68 ms |
22216 KB |
Output is correct |
42 |
Correct |
66 ms |
22220 KB |
Output is correct |
43 |
Correct |
66 ms |
22216 KB |
Output is correct |
44 |
Correct |
72 ms |
22260 KB |
Output is correct |
45 |
Correct |
116 ms |
27072 KB |
Output is correct |
46 |
Correct |
85 ms |
22464 KB |
Output is correct |
47 |
Correct |
66 ms |
20428 KB |
Output is correct |