Submission #678529

#TimeUsernameProblemLanguageResultExecution timeMemory
678529vjudge1Art Exhibition (JOI18_art)C++17
100 / 100
197 ms15952 KiB
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define cd complex<ld>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pld pair<ld,ld>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
ll n;
pll A[500005];
ll dp[500005];
ll Max[500005];
void solve()
{
    cin >> n;
    for(int i = 1; i<= n; i++)
    {
        cin >> A[i].fi >> A[i].se;
    }
    sort(A+1, A+n+1);
    for(int i = 1; i<= n; i++) dp[i] = dp[i-1]+A[i].se;
    Max[n] = dp[n]-A[n].fi;
    for(int i = n-1; i>= 1; i--)
    {
        Max[i] = dp[i]-A[i].fi;
        Max[i] = max(Max[i],Max[i+1]);
    }
    ll ans = LLONG_MIN;
    for(int i = n; i>= 1; i--)
    {
        ans = max(ans, Max[i]-dp[i-1]+A[i].fi);
    }
    cout << ans;
}
signed main()
{
    //freopen("IN.txt","r",stdin);
    //freopen("OUT.txt","w",stdout);
    ios_base::sync_with_stdio(NULL) ; cin.tie(nullptr) ; cout.tie(nullptr);
    ll t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
        cout << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...