# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
507544 | 2022-01-12T17:26:31 Z | maco1503 | Art Exhibition (JOI18_art) | C++14 | 0 ms | 0 KB |
#include <bits/stdc++.h> #define rpd ios_base :: sync_with_stdio(0); cin.tie(0); #define ll long long #define fs first #define sc second #define pb push_back #define mod 666013 #define NMAX 100000 + 5 #define inf LLONG_MAX using namespace std; pair < int ,int > v[500005]; int s; int st[500005]; int dr[500005]; int main() { rpd; int n; cin>>n; for(int i=1;i<=n;++i) { int mar,val; cin>>mar >> val; v[i]={mar,val}; s+=v[i].second; } sort(v+1,v+1+n); ll ans= s -v[n].first+v[1].first; long long sp=0; for(long long i=2;i<=n;++i) { sp+=v[i-1].sc; st[i]=v[i].fs-sp-v[1].fs; st[i]=max(st[i-1],st[i]); } sp=0; for(long long i=n-1;i>=1;--i) { sp+=v[i+1].sc; dr[i]=v[n].fs-sp-v[i].fs; dr[i]=max(dr[i],dr[i+1]); } for(int i=1;i<=n;i++)ans2=max(ans2,st[i]+dr[i]); cout<<ans+ans2; return 0; }