// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<ll>;
void maximize(auto& a, auto b){a = max(a, b);}
void minimize(auto& a, auto b){a = min(a, b);}
//#define int long long
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0};
const int MAXN = 1e6 + 10, LOG = 20, base = 311;
const int inf = 1e9 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
int n; ii a[MAXN]; ll f[MAXN];
void Solve(){
cin >> n;
For(i, 1, n) cin >> a[i].fi >> a[i].se;
sort(a + 1, a + n + 1, greater<ii>());
vector<ll> st(n * 2 + 5, +oo);
auto update = [&](int i, ll val){
for(st[i += n+1] = val; i > 1; i /= 2)
st[i / 2] = min(st[i ^ 1], st[i]);
};
auto get = [&](int l, int r){
ll res = oo;
for(l+=n+1, r+=n+2; l < r; l/=2, r/=2){
if(l & 1) minimize(res, st[l++]);
if(r & 1) minimize(res, st[--r]);
}
return res;
};
ll ans = -oo;
For(i, 1, n){
f[i] = f[i - 1] + a[i].se;
update(i, a[i].fi + f[i - 1]);
maximize(ans, f[i] + a[i].fi - get(1, i));
}
cout << ans << '\n';
}
int32_t main(){
cin.tie(0) -> sync_with_stdio(0);
if(fopen("JOI18_art.inp", "r")){
freopen("JOI18_art.inp", "r", stdin);
freopen("JOI18_art.out", "w", stdout);
}
int t = 1;
// cin >> t;
for (int test = 1; test <= t; test++){
Solve();
}
return 0;
}
Compilation message
art.cpp:20:15: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void maximize(auto& a, auto b){a = max(a, b);}
| ^~~~
art.cpp:20:24: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void maximize(auto& a, auto b){a = max(a, b);}
| ^~~~
art.cpp:21:15: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
21 | void minimize(auto& a, auto b){a = min(a, b);}
| ^~~~
art.cpp:21:24: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
21 | void minimize(auto& a, auto b){a = min(a, b);}
| ^~~~
art.cpp: In function 'int32_t main()':
art.cpp:62:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen("JOI18_art.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
art.cpp:63:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen("JOI18_art.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2552 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2636 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2384 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2552 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2636 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2384 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
3 ms |
2716 KB |
Output is correct |
29 |
Correct |
3 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2804 KB |
Output is correct |
32 |
Correct |
3 ms |
2640 KB |
Output is correct |
33 |
Correct |
3 ms |
2640 KB |
Output is correct |
34 |
Correct |
3 ms |
2640 KB |
Output is correct |
35 |
Correct |
3 ms |
2812 KB |
Output is correct |
36 |
Correct |
3 ms |
2640 KB |
Output is correct |
37 |
Correct |
2 ms |
2640 KB |
Output is correct |
38 |
Correct |
2 ms |
2816 KB |
Output is correct |
39 |
Correct |
3 ms |
2640 KB |
Output is correct |
40 |
Correct |
3 ms |
2640 KB |
Output is correct |
41 |
Correct |
3 ms |
2640 KB |
Output is correct |
42 |
Correct |
2 ms |
2740 KB |
Output is correct |
43 |
Correct |
3 ms |
2640 KB |
Output is correct |
44 |
Correct |
2 ms |
2640 KB |
Output is correct |
45 |
Correct |
3 ms |
2640 KB |
Output is correct |
46 |
Correct |
2 ms |
2640 KB |
Output is correct |
47 |
Correct |
2 ms |
2640 KB |
Output is correct |
48 |
Correct |
2 ms |
2532 KB |
Output is correct |
49 |
Correct |
3 ms |
2640 KB |
Output is correct |
50 |
Correct |
3 ms |
2640 KB |
Output is correct |
51 |
Correct |
3 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2552 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2636 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2384 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
3 ms |
2716 KB |
Output is correct |
29 |
Correct |
3 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2804 KB |
Output is correct |
32 |
Correct |
3 ms |
2640 KB |
Output is correct |
33 |
Correct |
3 ms |
2640 KB |
Output is correct |
34 |
Correct |
3 ms |
2640 KB |
Output is correct |
35 |
Correct |
3 ms |
2812 KB |
Output is correct |
36 |
Correct |
3 ms |
2640 KB |
Output is correct |
37 |
Correct |
2 ms |
2640 KB |
Output is correct |
38 |
Correct |
2 ms |
2816 KB |
Output is correct |
39 |
Correct |
3 ms |
2640 KB |
Output is correct |
40 |
Correct |
3 ms |
2640 KB |
Output is correct |
41 |
Correct |
3 ms |
2640 KB |
Output is correct |
42 |
Correct |
2 ms |
2740 KB |
Output is correct |
43 |
Correct |
3 ms |
2640 KB |
Output is correct |
44 |
Correct |
2 ms |
2640 KB |
Output is correct |
45 |
Correct |
3 ms |
2640 KB |
Output is correct |
46 |
Correct |
2 ms |
2640 KB |
Output is correct |
47 |
Correct |
2 ms |
2640 KB |
Output is correct |
48 |
Correct |
2 ms |
2532 KB |
Output is correct |
49 |
Correct |
3 ms |
2640 KB |
Output is correct |
50 |
Correct |
3 ms |
2640 KB |
Output is correct |
51 |
Correct |
3 ms |
2640 KB |
Output is correct |
52 |
Correct |
167 ms |
35656 KB |
Output is correct |
53 |
Correct |
188 ms |
35400 KB |
Output is correct |
54 |
Correct |
171 ms |
35400 KB |
Output is correct |
55 |
Correct |
173 ms |
35400 KB |
Output is correct |
56 |
Correct |
168 ms |
35400 KB |
Output is correct |
57 |
Correct |
163 ms |
35400 KB |
Output is correct |
58 |
Correct |
158 ms |
35400 KB |
Output is correct |
59 |
Correct |
160 ms |
35392 KB |
Output is correct |
60 |
Correct |
170 ms |
35404 KB |
Output is correct |
61 |
Correct |
172 ms |
35144 KB |
Output is correct |
62 |
Correct |
174 ms |
35288 KB |
Output is correct |
63 |
Correct |
168 ms |
35428 KB |
Output is correct |
64 |
Correct |
174 ms |
35304 KB |
Output is correct |
65 |
Correct |
171 ms |
35412 KB |
Output is correct |
66 |
Correct |
160 ms |
35400 KB |
Output is correct |
67 |
Correct |
154 ms |
35408 KB |
Output is correct |
68 |
Correct |
168 ms |
35400 KB |
Output is correct |