#include <bits/stdc++.h>
using namespace std;
/*
John Watson
https://codeforces.com/profile/quangminh98
Mua Code nhu mua Florentino !!
*/
#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
const ll oo = 1e18;
const int maxn = 5e5 + 9;
int n;
ll pre[maxn];
pair<ll, ll> a[maxn];
ll segment(int l, int r) { return (l <= r ? pre[r] - pre[l - 1] : 0); }
struct SegmentTree
{
int n;
vector<ll> st;
SegmentTree(int n) : n(n) { st.resize(4 * n + 5, 0); build(1, 1, n); }
void build(int head, int l, int r)
{
if (l == r)
{
st[head] = a[l].first + segment(l, n);
return;
}
int mid = l + r >> 1;
build(2 * head, l, mid);
build(2 * head + 1, mid + 1, r);
st[head] = max(st[2 * head], st[2 * head + 1]);
}
ll query(int head, int l, int r, int u, int v)
{
if (l > v || r < u) return -oo;
if (u <= l && r <= v) return st[head];
int mid = l + r >> 1;
return max(query(2 * head, l, mid, u, v), query(2 * head + 1, mid + 1, r, u, v));
}
ll query(int u, int v) { return query(1, 1, n, u, v); }
};
signed main()
{
if (fopen("test.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
faster();
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
// first: size
// second: value
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i].second;
SegmentTree seg(n);
ll ans = -oo;
for (int i = 1; i <= n; i++)
ans = max(ans, seg.query(1, i) - a[i].first - segment(i + 1, n));
cout << ans << '\n';
return 0;
}
Compilation message
art.cpp: In member function 'void SegmentTree::build(int, int, int)':
art.cpp:38:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
art.cpp: In member function 'long long int SegmentTree::query(int, int, int, int, int)':
art.cpp:50:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid = l + r >> 1;
| ~~^~~
art.cpp: In function 'int main()':
art.cpp:60:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
art.cpp:61:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
856 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
2 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
856 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
856 KB |
Output is correct |
42 |
Correct |
3 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
656 KB |
Output is correct |
44 |
Correct |
2 ms |
856 KB |
Output is correct |
45 |
Correct |
2 ms |
660 KB |
Output is correct |
46 |
Correct |
2 ms |
844 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
49 |
Correct |
2 ms |
860 KB |
Output is correct |
50 |
Correct |
2 ms |
724 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
856 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
2 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
2 ms |
856 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
856 KB |
Output is correct |
42 |
Correct |
3 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
656 KB |
Output is correct |
44 |
Correct |
2 ms |
856 KB |
Output is correct |
45 |
Correct |
2 ms |
660 KB |
Output is correct |
46 |
Correct |
2 ms |
844 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
49 |
Correct |
2 ms |
860 KB |
Output is correct |
50 |
Correct |
2 ms |
724 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
52 |
Correct |
173 ms |
40396 KB |
Output is correct |
53 |
Correct |
196 ms |
40296 KB |
Output is correct |
54 |
Correct |
170 ms |
40272 KB |
Output is correct |
55 |
Correct |
169 ms |
40276 KB |
Output is correct |
56 |
Correct |
155 ms |
40508 KB |
Output is correct |
57 |
Correct |
158 ms |
40316 KB |
Output is correct |
58 |
Correct |
160 ms |
40436 KB |
Output is correct |
59 |
Correct |
159 ms |
40276 KB |
Output is correct |
60 |
Correct |
153 ms |
40524 KB |
Output is correct |
61 |
Correct |
156 ms |
40276 KB |
Output is correct |
62 |
Correct |
162 ms |
40276 KB |
Output is correct |
63 |
Correct |
159 ms |
40392 KB |
Output is correct |
64 |
Correct |
158 ms |
40276 KB |
Output is correct |
65 |
Correct |
154 ms |
40320 KB |
Output is correct |
66 |
Correct |
152 ms |
40276 KB |
Output is correct |
67 |
Correct |
155 ms |
40276 KB |
Output is correct |
68 |
Correct |
160 ms |
40300 KB |
Output is correct |