#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e5 + 2;
const int M = 1e9;
int mod = 1000000007;
ll st[4 * N];
void Set(int node, int l, int r, int x, ll y) {
if (l == r) {st[node] = y; return;}
int mid = l + r >> 1;
if (x <= mid) Set(2 * node, l, mid, x, y);
else Set(2 * node + 1, mid + 1, r, x, y);
st[node] = max(st[2 * node], st[2 * node + 1]);
}
ll Get(int node, int l, int r, int ql, int qr) {
if (r < ql || qr < l) return 0;
if (ql <= l && r <= qr) return st[node];
int mid = l + r >> 1;
return max(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<array<ll, 2>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i][0] >> a[i][1];
}
sort(a.begin(), a.end());
for (int i = n - 2; i >= 0; i--) a[i][1] += a[i + 1][1];
for (int i = 0; i < n; i++) Set(1, 0, n - 1, i, a[i][0] + a[i][1]);
ll ans = 0;
for (int i = n - 1; i >= 0; i--) {
ll x = Get(1, 0, n - 1, 0, i);
if (i == n - 1) smax(ans, x - a[i][0]);
else smax(ans, x - a[i][0] - a[i + 1][1]);
}
cout << ans << en;
return 0;
}
Compilation message
art.cpp: In function 'void Set(int, int, int, int, long long int)':
art.cpp:15:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | int mid = l + r >> 1;
| ~~^~~
art.cpp: In function 'long long int Get(int, int, int, int, int)':
art.cpp:23:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
5 ms |
596 KB |
Output is correct |
29 |
Correct |
6 ms |
596 KB |
Output is correct |
30 |
Correct |
5 ms |
644 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
4 ms |
596 KB |
Output is correct |
33 |
Correct |
5 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
652 KB |
Output is correct |
35 |
Correct |
4 ms |
592 KB |
Output is correct |
36 |
Correct |
4 ms |
596 KB |
Output is correct |
37 |
Correct |
3 ms |
596 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
6 ms |
596 KB |
Output is correct |
40 |
Correct |
4 ms |
596 KB |
Output is correct |
41 |
Correct |
3 ms |
600 KB |
Output is correct |
42 |
Correct |
5 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
596 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
4 ms |
644 KB |
Output is correct |
46 |
Correct |
4 ms |
600 KB |
Output is correct |
47 |
Correct |
7 ms |
644 KB |
Output is correct |
48 |
Correct |
6 ms |
596 KB |
Output is correct |
49 |
Correct |
5 ms |
596 KB |
Output is correct |
50 |
Correct |
3 ms |
648 KB |
Output is correct |
51 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
5 ms |
596 KB |
Output is correct |
29 |
Correct |
6 ms |
596 KB |
Output is correct |
30 |
Correct |
5 ms |
644 KB |
Output is correct |
31 |
Correct |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
4 ms |
596 KB |
Output is correct |
33 |
Correct |
5 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
652 KB |
Output is correct |
35 |
Correct |
4 ms |
592 KB |
Output is correct |
36 |
Correct |
4 ms |
596 KB |
Output is correct |
37 |
Correct |
3 ms |
596 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
6 ms |
596 KB |
Output is correct |
40 |
Correct |
4 ms |
596 KB |
Output is correct |
41 |
Correct |
3 ms |
600 KB |
Output is correct |
42 |
Correct |
5 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
596 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
4 ms |
644 KB |
Output is correct |
46 |
Correct |
4 ms |
600 KB |
Output is correct |
47 |
Correct |
7 ms |
644 KB |
Output is correct |
48 |
Correct |
6 ms |
596 KB |
Output is correct |
49 |
Correct |
5 ms |
596 KB |
Output is correct |
50 |
Correct |
3 ms |
648 KB |
Output is correct |
51 |
Correct |
3 ms |
596 KB |
Output is correct |
52 |
Correct |
373 ms |
21680 KB |
Output is correct |
53 |
Correct |
376 ms |
21552 KB |
Output is correct |
54 |
Correct |
405 ms |
21680 KB |
Output is correct |
55 |
Correct |
398 ms |
21572 KB |
Output is correct |
56 |
Correct |
459 ms |
21552 KB |
Output is correct |
57 |
Correct |
404 ms |
21592 KB |
Output is correct |
58 |
Correct |
395 ms |
21616 KB |
Output is correct |
59 |
Correct |
390 ms |
23920 KB |
Output is correct |
60 |
Correct |
317 ms |
24868 KB |
Output is correct |
61 |
Correct |
316 ms |
24908 KB |
Output is correct |
62 |
Correct |
344 ms |
24984 KB |
Output is correct |
63 |
Correct |
334 ms |
24060 KB |
Output is correct |
64 |
Correct |
308 ms |
24780 KB |
Output is correct |
65 |
Correct |
324 ms |
24884 KB |
Output is correct |
66 |
Correct |
305 ms |
24860 KB |
Output is correct |
67 |
Correct |
317 ms |
24784 KB |
Output is correct |
68 |
Correct |
349 ms |
24852 KB |
Output is correct |