#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
const int MOD = 1e9+7;
#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
void FASTIO() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
const int MXN = 5e5+5;
int n;
ll A[MXN], B[MXN];
int main() {
FASTIO();
cin >> n;
for(int i = 1; i <= n; i++) cin >> A[i] >> B[i], A[i] -= B[i], A[i] += A[i-1];
priority_queue<ll> PQ;
ll sy = 0;
for(int i = 1; i < n; i++) {
if(A[i] < 0) sy += -A[i], A[i] = 0;
sy += A[i];
PQ.push(A[i]);
PQ.push(A[i]);
PQ.pop();
}
ll ans = sy;
while(!PQ.empty()) {
ans -= min(A[n], PQ.top());
PQ.pop();
}
cout << ans << "\n";
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
13 ms |
2008 KB |
Output is correct |
5 |
Correct |
32 ms |
3660 KB |
Output is correct |
6 |
Correct |
100 ms |
9764 KB |
Output is correct |
7 |
Correct |
130 ms |
19044 KB |
Output is correct |
8 |
Correct |
159 ms |
17232 KB |
Output is correct |
9 |
Correct |
138 ms |
16524 KB |
Output is correct |
10 |
Correct |
105 ms |
14280 KB |
Output is correct |
11 |
Correct |
103 ms |
14296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
13 ms |
2008 KB |
Output is correct |
5 |
Correct |
32 ms |
3660 KB |
Output is correct |
6 |
Correct |
100 ms |
9764 KB |
Output is correct |
7 |
Correct |
130 ms |
19044 KB |
Output is correct |
8 |
Correct |
159 ms |
17232 KB |
Output is correct |
9 |
Correct |
138 ms |
16524 KB |
Output is correct |
10 |
Correct |
105 ms |
14280 KB |
Output is correct |
11 |
Correct |
103 ms |
14296 KB |
Output is correct |
12 |
Correct |
43 ms |
5076 KB |
Output is correct |
13 |
Correct |
106 ms |
13228 KB |
Output is correct |
14 |
Correct |
148 ms |
19068 KB |
Output is correct |
15 |
Correct |
178 ms |
17160 KB |
Output is correct |
16 |
Correct |
168 ms |
16548 KB |
Output is correct |
17 |
Correct |
129 ms |
14320 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
13 ms |
2008 KB |
Output is correct |
12 |
Correct |
32 ms |
3660 KB |
Output is correct |
13 |
Correct |
100 ms |
9764 KB |
Output is correct |
14 |
Correct |
130 ms |
19044 KB |
Output is correct |
15 |
Correct |
159 ms |
17232 KB |
Output is correct |
16 |
Correct |
138 ms |
16524 KB |
Output is correct |
17 |
Correct |
105 ms |
14280 KB |
Output is correct |
18 |
Correct |
103 ms |
14296 KB |
Output is correct |
19 |
Correct |
43 ms |
5076 KB |
Output is correct |
20 |
Correct |
106 ms |
13228 KB |
Output is correct |
21 |
Correct |
148 ms |
19068 KB |
Output is correct |
22 |
Correct |
178 ms |
17160 KB |
Output is correct |
23 |
Correct |
168 ms |
16548 KB |
Output is correct |
24 |
Correct |
129 ms |
14320 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
48 ms |
5068 KB |
Output is correct |
34 |
Correct |
109 ms |
13232 KB |
Output is correct |
35 |
Correct |
202 ms |
19164 KB |
Output is correct |
36 |
Correct |
191 ms |
16708 KB |
Output is correct |
37 |
Correct |
153 ms |
17224 KB |
Output is correct |
38 |
Correct |
225 ms |
19164 KB |
Output is correct |
39 |
Correct |
121 ms |
15320 KB |
Output is correct |
40 |
Correct |
136 ms |
14404 KB |
Output is correct |
41 |
Correct |
101 ms |
14288 KB |
Output is correct |
42 |
Correct |
105 ms |
14280 KB |
Output is correct |
43 |
Correct |
100 ms |
14352 KB |
Output is correct |
44 |
Correct |
101 ms |
14316 KB |
Output is correct |
45 |
Correct |
176 ms |
19100 KB |
Output is correct |
46 |
Correct |
140 ms |
14680 KB |
Output is correct |
47 |
Correct |
126 ms |
13256 KB |
Output is correct |