#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 5e5 + 5, mod = 1e9 + 7; // !
int t, d[N];
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n;
cin >> n;
// ans = jami(ai[i] - a[i]) = jami (d'[i] - d[i]);
priority_queue<int, vector<int>, less<int> > q;
int ans = 0;
for(int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
// a' - b >= 0 yvelastvis => d'[i + 1] >= d'[i]
d[i] = d[i - 1] + a - b;
if(d[i] < 0) ans += -d[i];
q.push(max(0ll, d[i]));
q.push(max(0ll, d[i]));
ans += q.top() - (max(0ll, d[i]));
q.pop();
}
q.push(d[n]);
int k = 1;
while(q.top() != d[n]) {
int x = q.top();
q.pop();
ans += k * (x - q.top());
++k;
}
cout << ans;
}
Compilation message
bulves.cpp:9:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
9 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
14 ms |
1536 KB |
Output is correct |
5 |
Correct |
27 ms |
2612 KB |
Output is correct |
6 |
Correct |
102 ms |
7644 KB |
Output is correct |
7 |
Correct |
110 ms |
14940 KB |
Output is correct |
8 |
Correct |
161 ms |
13084 KB |
Output is correct |
9 |
Correct |
113 ms |
12480 KB |
Output is correct |
10 |
Correct |
94 ms |
10212 KB |
Output is correct |
11 |
Correct |
107 ms |
10140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
14 ms |
1536 KB |
Output is correct |
5 |
Correct |
27 ms |
2612 KB |
Output is correct |
6 |
Correct |
102 ms |
7644 KB |
Output is correct |
7 |
Correct |
110 ms |
14940 KB |
Output is correct |
8 |
Correct |
161 ms |
13084 KB |
Output is correct |
9 |
Correct |
113 ms |
12480 KB |
Output is correct |
10 |
Correct |
94 ms |
10212 KB |
Output is correct |
11 |
Correct |
107 ms |
10140 KB |
Output is correct |
12 |
Correct |
42 ms |
4004 KB |
Output is correct |
13 |
Correct |
118 ms |
10308 KB |
Output is correct |
14 |
Correct |
114 ms |
14980 KB |
Output is correct |
15 |
Correct |
156 ms |
13156 KB |
Output is correct |
16 |
Correct |
172 ms |
12388 KB |
Output is correct |
17 |
Correct |
94 ms |
10288 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
14 ms |
1536 KB |
Output is correct |
12 |
Correct |
27 ms |
2612 KB |
Output is correct |
13 |
Correct |
102 ms |
7644 KB |
Output is correct |
14 |
Correct |
110 ms |
14940 KB |
Output is correct |
15 |
Correct |
161 ms |
13084 KB |
Output is correct |
16 |
Correct |
113 ms |
12480 KB |
Output is correct |
17 |
Correct |
94 ms |
10212 KB |
Output is correct |
18 |
Correct |
107 ms |
10140 KB |
Output is correct |
19 |
Correct |
42 ms |
4004 KB |
Output is correct |
20 |
Correct |
118 ms |
10308 KB |
Output is correct |
21 |
Correct |
114 ms |
14980 KB |
Output is correct |
22 |
Correct |
156 ms |
13156 KB |
Output is correct |
23 |
Correct |
172 ms |
12388 KB |
Output is correct |
24 |
Correct |
94 ms |
10288 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
38 ms |
4076 KB |
Output is correct |
34 |
Correct |
86 ms |
10308 KB |
Output is correct |
35 |
Correct |
138 ms |
14988 KB |
Output is correct |
36 |
Correct |
129 ms |
12540 KB |
Output is correct |
37 |
Correct |
147 ms |
13112 KB |
Output is correct |
38 |
Correct |
134 ms |
15048 KB |
Output is correct |
39 |
Correct |
89 ms |
11212 KB |
Output is correct |
40 |
Correct |
102 ms |
10296 KB |
Output is correct |
41 |
Correct |
118 ms |
10212 KB |
Output is correct |
42 |
Correct |
86 ms |
10176 KB |
Output is correct |
43 |
Correct |
86 ms |
10180 KB |
Output is correct |
44 |
Correct |
89 ms |
10248 KB |
Output is correct |
45 |
Correct |
158 ms |
15020 KB |
Output is correct |
46 |
Correct |
135 ms |
10432 KB |
Output is correct |
47 |
Correct |
92 ms |
9164 KB |
Output is correct |