#include <bits/stdc++.h>
using namespace std;
#define fod(i,a,b) for(int i = (int) (a); i <= (int) (b); i++)
#define fok(i,a,b) for(int i = (int) (a); i >= (int) (b); i--)
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define ve vector
#define vi ve<int>
#define vll ve<ll>
#define el '\n'
#define mask(i) (1LL<<(i))
#define BIT(msk,i) (msk>>(i)&1LL)
template<class T> bool mini(T &a, T b){ return (a > (b) ? a = (b), 1 : 0); }
template<class T> bool maxi(T &a, T b){ return (a < (b) ? a = (b), 1 : 0); }
const int base = mask(20) + 5;
#define name "art"
int n;
const int N = 5e5;
pair<ll, ll> a[N + 5];
namespace sub1{
bool check(){
return n <= 16;
}
void solve(){
ll res = 0;
fod(msk,1,mask(n)-1){
ll sum = 0;
ll mx = -1e18;
ll mn = 1e18;
fod(i,1,n) if(BIT(msk,i-1)){
sum += a[i].se;
maxi(mx, a[i].fi);
mini(mn, a[i].fi);
}
maxi(res, sum + mn - mx);
}
cout << res;
}
}
namespace sub2{
bool check(){
return n <= 5000;
}
ll prefix_sum[N + 5];
void solve(){
sort(a + 1, a + n + 1);
fod(i,1,n) prefix_sum[i] = prefix_sum[i-1] + a[i].se;
ll res = 0;
fod(l,1,n) fod(r,l,n){
maxi(res, prefix_sum[r] - prefix_sum[l-1] + a[l].fi - a[r].fi);
}
cout << res;
}
}
namespace sub3{
ll res = 0;
ll dp[N + 5][2];
void solve(){
sort(a + 1, a + n + 1);
memset(dp, -0x3f, sizeof dp);
const ll big = dp[0][0];
dp[0][0] = 0;
fod(i,1,n){
if(dp[i-1][0] != big){
maxi(dp[i][0], dp[i-1][0]);
maxi(dp[i][1], dp[i-1][0] + a[i].fi + a[i].se);
}
if(dp[i-1][1] != big){
maxi(dp[i][1], dp[i-1][1] + a[i].se);
}
maxi(res, dp[i][1] - a[i].fi);
}
cout << res;
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if(fopen(name".inp", "r")){
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
cin >> n;
fod(i,1,n) cin >> a[i].fi >> a[i].se;
if(sub1 :: check()) sub1 :: solve();
else if(sub2 :: check()) sub2 :: solve();
else sub3 :: solve();
return 0;
}
Compilation message
art.cpp: In function 'int main()':
art.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
art.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2608 KB |
Output is correct |
3 |
Correct |
4 ms |
2384 KB |
Output is correct |
4 |
Correct |
4 ms |
2384 KB |
Output is correct |
5 |
Correct |
4 ms |
2384 KB |
Output is correct |
6 |
Correct |
4 ms |
2384 KB |
Output is correct |
7 |
Correct |
4 ms |
2384 KB |
Output is correct |
8 |
Correct |
4 ms |
2384 KB |
Output is correct |
9 |
Correct |
4 ms |
2384 KB |
Output is correct |
10 |
Correct |
4 ms |
2516 KB |
Output is correct |
11 |
Correct |
5 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2608 KB |
Output is correct |
3 |
Correct |
4 ms |
2384 KB |
Output is correct |
4 |
Correct |
4 ms |
2384 KB |
Output is correct |
5 |
Correct |
4 ms |
2384 KB |
Output is correct |
6 |
Correct |
4 ms |
2384 KB |
Output is correct |
7 |
Correct |
4 ms |
2384 KB |
Output is correct |
8 |
Correct |
4 ms |
2384 KB |
Output is correct |
9 |
Correct |
4 ms |
2384 KB |
Output is correct |
10 |
Correct |
4 ms |
2516 KB |
Output is correct |
11 |
Correct |
5 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2388 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 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 |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2416 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 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 |
2 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2608 KB |
Output is correct |
3 |
Correct |
4 ms |
2384 KB |
Output is correct |
4 |
Correct |
4 ms |
2384 KB |
Output is correct |
5 |
Correct |
4 ms |
2384 KB |
Output is correct |
6 |
Correct |
4 ms |
2384 KB |
Output is correct |
7 |
Correct |
4 ms |
2384 KB |
Output is correct |
8 |
Correct |
4 ms |
2384 KB |
Output is correct |
9 |
Correct |
4 ms |
2384 KB |
Output is correct |
10 |
Correct |
4 ms |
2516 KB |
Output is correct |
11 |
Correct |
5 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2388 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 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 |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2416 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 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 |
2 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
11 ms |
2652 KB |
Output is correct |
28 |
Correct |
11 ms |
2652 KB |
Output is correct |
29 |
Correct |
11 ms |
2652 KB |
Output is correct |
30 |
Correct |
11 ms |
2704 KB |
Output is correct |
31 |
Correct |
11 ms |
2808 KB |
Output is correct |
32 |
Correct |
12 ms |
2644 KB |
Output is correct |
33 |
Correct |
11 ms |
2708 KB |
Output is correct |
34 |
Correct |
11 ms |
2656 KB |
Output is correct |
35 |
Correct |
11 ms |
2716 KB |
Output is correct |
36 |
Correct |
11 ms |
2812 KB |
Output is correct |
37 |
Correct |
11 ms |
2708 KB |
Output is correct |
38 |
Correct |
11 ms |
2656 KB |
Output is correct |
39 |
Correct |
11 ms |
2656 KB |
Output is correct |
40 |
Correct |
11 ms |
2808 KB |
Output is correct |
41 |
Correct |
11 ms |
2708 KB |
Output is correct |
42 |
Correct |
11 ms |
2656 KB |
Output is correct |
43 |
Correct |
11 ms |
2908 KB |
Output is correct |
44 |
Correct |
11 ms |
2652 KB |
Output is correct |
45 |
Correct |
11 ms |
2652 KB |
Output is correct |
46 |
Correct |
11 ms |
2652 KB |
Output is correct |
47 |
Correct |
11 ms |
2704 KB |
Output is correct |
48 |
Correct |
11 ms |
2652 KB |
Output is correct |
49 |
Correct |
11 ms |
2812 KB |
Output is correct |
50 |
Correct |
11 ms |
2652 KB |
Output is correct |
51 |
Correct |
11 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2608 KB |
Output is correct |
3 |
Correct |
4 ms |
2384 KB |
Output is correct |
4 |
Correct |
4 ms |
2384 KB |
Output is correct |
5 |
Correct |
4 ms |
2384 KB |
Output is correct |
6 |
Correct |
4 ms |
2384 KB |
Output is correct |
7 |
Correct |
4 ms |
2384 KB |
Output is correct |
8 |
Correct |
4 ms |
2384 KB |
Output is correct |
9 |
Correct |
4 ms |
2384 KB |
Output is correct |
10 |
Correct |
4 ms |
2516 KB |
Output is correct |
11 |
Correct |
5 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2388 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 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 |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2416 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 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 |
2 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
11 ms |
2652 KB |
Output is correct |
28 |
Correct |
11 ms |
2652 KB |
Output is correct |
29 |
Correct |
11 ms |
2652 KB |
Output is correct |
30 |
Correct |
11 ms |
2704 KB |
Output is correct |
31 |
Correct |
11 ms |
2808 KB |
Output is correct |
32 |
Correct |
12 ms |
2644 KB |
Output is correct |
33 |
Correct |
11 ms |
2708 KB |
Output is correct |
34 |
Correct |
11 ms |
2656 KB |
Output is correct |
35 |
Correct |
11 ms |
2716 KB |
Output is correct |
36 |
Correct |
11 ms |
2812 KB |
Output is correct |
37 |
Correct |
11 ms |
2708 KB |
Output is correct |
38 |
Correct |
11 ms |
2656 KB |
Output is correct |
39 |
Correct |
11 ms |
2656 KB |
Output is correct |
40 |
Correct |
11 ms |
2808 KB |
Output is correct |
41 |
Correct |
11 ms |
2708 KB |
Output is correct |
42 |
Correct |
11 ms |
2656 KB |
Output is correct |
43 |
Correct |
11 ms |
2908 KB |
Output is correct |
44 |
Correct |
11 ms |
2652 KB |
Output is correct |
45 |
Correct |
11 ms |
2652 KB |
Output is correct |
46 |
Correct |
11 ms |
2652 KB |
Output is correct |
47 |
Correct |
11 ms |
2704 KB |
Output is correct |
48 |
Correct |
11 ms |
2652 KB |
Output is correct |
49 |
Correct |
11 ms |
2812 KB |
Output is correct |
50 |
Correct |
11 ms |
2652 KB |
Output is correct |
51 |
Correct |
11 ms |
2652 KB |
Output is correct |
52 |
Correct |
122 ms |
30548 KB |
Output is correct |
53 |
Correct |
131 ms |
30568 KB |
Output is correct |
54 |
Correct |
123 ms |
30668 KB |
Output is correct |
55 |
Correct |
134 ms |
30412 KB |
Output is correct |
56 |
Correct |
124 ms |
30528 KB |
Output is correct |
57 |
Correct |
121 ms |
30536 KB |
Output is correct |
58 |
Correct |
124 ms |
30536 KB |
Output is correct |
59 |
Correct |
124 ms |
30536 KB |
Output is correct |
60 |
Correct |
125 ms |
30536 KB |
Output is correct |
61 |
Correct |
121 ms |
30412 KB |
Output is correct |
62 |
Correct |
122 ms |
30532 KB |
Output is correct |
63 |
Correct |
131 ms |
30536 KB |
Output is correct |
64 |
Correct |
123 ms |
30656 KB |
Output is correct |
65 |
Correct |
117 ms |
30536 KB |
Output is correct |
66 |
Correct |
116 ms |
30536 KB |
Output is correct |
67 |
Correct |
115 ms |
30536 KB |
Output is correct |
68 |
Correct |
115 ms |
30536 KB |
Output is correct |