#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC target("avx2")
using namespace std;
#define pb push_back
#define sz size()
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define int long long
typedef pair < int, int > pii;
typedef vector < int > vi;
typedef vector < vi > vvi;
const int N = 500200;
int pref[N];
int t[4 * N];
void upd (int v, int l, int r, int pos, int val) {
if (l > pos || r < pos) {
return;
}
if (l == r) {
t[v] = val;
return;
}
int mid = (l + r) / 2;
upd (2 * v, l, mid, pos, val);
upd (2 * v + 1, mid + 1, r, pos, val);
t[v] = max(t[2 * v], t[2 * v + 1]);
}
int get_max (int v, int l, int r, int tl, int tr) {
if (l > tr || r < tl) {
return -1e18;
}
if (l >= tl && r <= tr) {
return t[v];
}
int mid = (l + r) / 2;
return max(get_max(2 * v, l, mid, tl, tr), get_max(2 * v + 1, mid + 1, r, tl, tr));
}
void solve(){
int n;
cin >> n;
pii a[n + 7];
for (int i = 1; i <= n; i++) {
cin >> a[i].F >> a[i].S;
}
sort (a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + a[i].S;
upd (1, 1, n, i, pref[i] - a[i].F);
}
int ans = 0;
for (int i = 1; i <= n; i++){
int mx = get_max(1, 1, n, i, n);
ans = max(ans, mx - pref[i - 1] + a[i].F);
// cout << i << " " << mx << " " << ans << "\n";
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
3 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
468 KB |
Output is correct |
36 |
Correct |
3 ms |
468 KB |
Output is correct |
37 |
Correct |
3 ms |
584 KB |
Output is correct |
38 |
Correct |
3 ms |
468 KB |
Output is correct |
39 |
Correct |
3 ms |
468 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
468 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
3 ms |
468 KB |
Output is correct |
46 |
Correct |
3 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
468 KB |
Output is correct |
48 |
Correct |
3 ms |
528 KB |
Output is correct |
49 |
Correct |
3 ms |
468 KB |
Output is correct |
50 |
Correct |
3 ms |
468 KB |
Output is correct |
51 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
468 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
3 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
468 KB |
Output is correct |
36 |
Correct |
3 ms |
468 KB |
Output is correct |
37 |
Correct |
3 ms |
584 KB |
Output is correct |
38 |
Correct |
3 ms |
468 KB |
Output is correct |
39 |
Correct |
3 ms |
468 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
468 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
3 ms |
468 KB |
Output is correct |
46 |
Correct |
3 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
468 KB |
Output is correct |
48 |
Correct |
3 ms |
528 KB |
Output is correct |
49 |
Correct |
3 ms |
468 KB |
Output is correct |
50 |
Correct |
3 ms |
468 KB |
Output is correct |
51 |
Correct |
3 ms |
468 KB |
Output is correct |
52 |
Correct |
256 ms |
20180 KB |
Output is correct |
53 |
Correct |
290 ms |
32904 KB |
Output is correct |
54 |
Correct |
272 ms |
32808 KB |
Output is correct |
55 |
Correct |
277 ms |
32908 KB |
Output is correct |
56 |
Correct |
256 ms |
32812 KB |
Output is correct |
57 |
Correct |
264 ms |
33028 KB |
Output is correct |
58 |
Correct |
271 ms |
32832 KB |
Output is correct |
59 |
Correct |
264 ms |
32824 KB |
Output is correct |
60 |
Correct |
262 ms |
32832 KB |
Output is correct |
61 |
Correct |
255 ms |
32692 KB |
Output is correct |
62 |
Correct |
275 ms |
32952 KB |
Output is correct |
63 |
Correct |
293 ms |
32820 KB |
Output is correct |
64 |
Correct |
272 ms |
32940 KB |
Output is correct |
65 |
Correct |
259 ms |
32836 KB |
Output is correct |
66 |
Correct |
283 ms |
32824 KB |
Output is correct |
67 |
Correct |
267 ms |
32892 KB |
Output is correct |
68 |
Correct |
265 ms |
32836 KB |
Output is correct |