#include <bits/stdc++.h>
using namespace std;
void solve_2_n(vector <pair<long long,long long>> v){
long long best=LLONG_MIN;
long long n=v.size();
for(long long mask=0; mask<(1<<n); mask++){
long long S=0;
long long amin=-1,amax=0;
for(long long i=0;i<n;i++){
if(mask & (1<<i)){
if (amin==-1){amin=v[i].first;}
amin=min(amin,v[i].first);
amax=max(amax,v[i].first);
S+=v[i].second;
}
}
best=max(best,S+amin-amax);
}
cout<<best<<"\n";
}
void solve_2d_dp(vector <pair<long long,long long>> v){
long long n=v.size();
long long dp[n][n];
sort(v.begin(),v.end());
long long psum[n+1];
psum[0]=0;
for (int i=1; i<=n; i++){
psum[i]=psum[i-1]+v[i-1].second;
}
long long best=0;
for (int i=0; i<n; i++){
for (int j=i; j<n; j++){
best=max(best,psum[j+1]-psum[i]+v[i].first-v[j].first);
}
}
cout<<best<<"\n";
}
void solve_1d_dp(vector <pair<long long,long long>> v){
long long n=v.size();
sort(v.begin(),v.end());
long long psum[n+1];
psum[0]=0;
for (int i=1; i<=n; i++){
psum[i]=psum[i-1]+v[i-1].second;
}
long long best=0;
long long mx=LLONG_MIN;
for (int i=0; i<n; i++){
mx=max(mx,v[i].first-psum[i]);
best=max(best,psum[i]+v[i].second-v[i].first+mx);
}
cout<<best<<" ";
}
int main(){
long long n,tm1,tm2;
cin>>n;
vector <pair<long long,long long>> v;
for (long long i=0; i<n; i++){
cin>>tm1>>tm2;
v.push_back({tm1,tm2});
}
//solve_2_n(v);
//solve_2d_dp(v);
solve_1d_dp(v);
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |