#include<bits/stdc++.h>
#define en "\n"
#define s second
#define f first
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define vi vector<int>
#define vii vector<pair<int,int>>
#define int long long
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define loop(a) for(int i = 0; i < a; i++)
#define loopv(i,a) for (int i = 0; i < a; i++)
#define all(x) (x).begin(), (x).end()
#define prDouble(x) printf("%.8f", x)
#define goog(tno) printf("Case #%d: ", tno)
using namespace std;
const int N = 500100;
int t[N*4];
void build (vector <int> a, int v, int tl, int tr) {
if (tl == tr)
t[v] = a[tl];
else {
int tm = (tl + tr) / 2;
build (a, v*2, tl, tm);
build (a, v*2+1, tm+1, tr);
t[v] = max(t[v*2], t[v*2+1]);
}
}
int sum (int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr)
return t[v];
int tm = (tl + tr) / 2;
return max(sum (v*2, tl, tm, l, min(r,tm))
, sum (v*2+1, tm+1, tr, max(l,tm+1), r));
}
void solve() {
int n;
cin>>n;
vii a(n);
for(int i=0; i<n; i++){
cin>>a[i].f>>a[i].s;
}
sort(all(a));
for(int i=0; i<a.size()-1; i++){
if(a[i].f==a[i+1].f){
a[i+1].s+=a[i].s;
a.erase(a.begin()+i);
i--;
}
}
n=a.size();
// for(int i=0; i<n; i++){
// cout<<a[i].f<<" "<<a[i].s<<en;
// }
vi p(n+1, 0), pl(n+1, 0), pr(n+1, 0);
for(int i=0; i<n; i++){
p[i+1] = p[i] + a[i].s;
}
for(int i=1; i<=n; i++){
pl[i] = a[i-1].f - p[i-1];
}
for(int i=1; i<=n; i++){
pr[i] = p[i] - a[i-1].f;
}
// for(int i=0; i<n; i++){
// cout<<a[i].f<<" "<<a[i].s<<" "<<p[i+1]<<" "<<pl[i+1]<<" "<<pr[i+1]<<en;
// }
build(pl, 1, 0, n);
int mx1=0, mx2=0;
int ans=0;
for(int i=0; i<=n; i++){
ans=max(ans, sum(1, 0, n, 0, i) + pr[i]);
}
cout<<ans;
}
signed main() {
fast_io;
int tc = 1;
// scanf("%d", &tc);
while (tc--)
solve();
}
Compilation message
art.cpp: In function 'void solve()':
art.cpp:47:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=0; i<a.size()-1; i++){
| ~^~~~~~~~~~~
art.cpp:73:9: warning: unused variable 'mx1' [-Wunused-variable]
73 | int mx1=0, mx2=0;
| ^~~
art.cpp:73:16: warning: unused variable 'mx2' [-Wunused-variable]
73 | int mx1=0, mx2=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
36 ms |
1324 KB |
Output is correct |
28 |
Correct |
42 ms |
1336 KB |
Output is correct |
29 |
Correct |
36 ms |
1316 KB |
Output is correct |
30 |
Correct |
42 ms |
1304 KB |
Output is correct |
31 |
Correct |
38 ms |
1300 KB |
Output is correct |
32 |
Correct |
40 ms |
1320 KB |
Output is correct |
33 |
Correct |
36 ms |
1300 KB |
Output is correct |
34 |
Correct |
36 ms |
1324 KB |
Output is correct |
35 |
Correct |
37 ms |
4780 KB |
Output is correct |
36 |
Correct |
42 ms |
1296 KB |
Output is correct |
37 |
Correct |
37 ms |
4708 KB |
Output is correct |
38 |
Correct |
38 ms |
3228 KB |
Output is correct |
39 |
Correct |
37 ms |
1296 KB |
Output is correct |
40 |
Correct |
36 ms |
1300 KB |
Output is correct |
41 |
Correct |
35 ms |
1324 KB |
Output is correct |
42 |
Correct |
36 ms |
1300 KB |
Output is correct |
43 |
Correct |
63 ms |
1304 KB |
Output is correct |
44 |
Correct |
35 ms |
1304 KB |
Output is correct |
45 |
Correct |
35 ms |
1312 KB |
Output is correct |
46 |
Correct |
38 ms |
1316 KB |
Output is correct |
47 |
Correct |
35 ms |
1304 KB |
Output is correct |
48 |
Correct |
35 ms |
1300 KB |
Output is correct |
49 |
Correct |
37 ms |
1324 KB |
Output is correct |
50 |
Correct |
47 ms |
1304 KB |
Output is correct |
51 |
Correct |
41 ms |
1316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
36 ms |
1324 KB |
Output is correct |
28 |
Correct |
42 ms |
1336 KB |
Output is correct |
29 |
Correct |
36 ms |
1316 KB |
Output is correct |
30 |
Correct |
42 ms |
1304 KB |
Output is correct |
31 |
Correct |
38 ms |
1300 KB |
Output is correct |
32 |
Correct |
40 ms |
1320 KB |
Output is correct |
33 |
Correct |
36 ms |
1300 KB |
Output is correct |
34 |
Correct |
36 ms |
1324 KB |
Output is correct |
35 |
Correct |
37 ms |
4780 KB |
Output is correct |
36 |
Correct |
42 ms |
1296 KB |
Output is correct |
37 |
Correct |
37 ms |
4708 KB |
Output is correct |
38 |
Correct |
38 ms |
3228 KB |
Output is correct |
39 |
Correct |
37 ms |
1296 KB |
Output is correct |
40 |
Correct |
36 ms |
1300 KB |
Output is correct |
41 |
Correct |
35 ms |
1324 KB |
Output is correct |
42 |
Correct |
36 ms |
1300 KB |
Output is correct |
43 |
Correct |
63 ms |
1304 KB |
Output is correct |
44 |
Correct |
35 ms |
1304 KB |
Output is correct |
45 |
Correct |
35 ms |
1312 KB |
Output is correct |
46 |
Correct |
38 ms |
1316 KB |
Output is correct |
47 |
Correct |
35 ms |
1304 KB |
Output is correct |
48 |
Correct |
35 ms |
1300 KB |
Output is correct |
49 |
Correct |
37 ms |
1324 KB |
Output is correct |
50 |
Correct |
47 ms |
1304 KB |
Output is correct |
51 |
Correct |
41 ms |
1316 KB |
Output is correct |
52 |
Execution timed out |
1092 ms |
102628 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |