#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);
}
}
}
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:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
art.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |