This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define int long long
const int N = 5e5 + 10;
ll suf_max[N];
void solve()
{
for(int i = 0;i < N; ++i){
suf_max[i] = LLONG_MIN;
}
int n;
cin >> n;
vector<pair<int,int>> a(n);
vector<int> pref(n);
for(int i = 0;i < n; ++i){
cin >> a[i].first >> a[i].second;
}
sort(all(a));
for(int i = 0;i < n; ++i){
pref[i] = a[i].second;
if(i)pref[i] += pref[i - 1];
}
for(int i = n - 1;i >= 0; --i){
suf_max[i] = max(suf_max[i + 1], pref[i] - a[i].first);
}
ll ans = 0;
for(int i = 0;i < n; ++i){
ans = max(ans, a[i].second);
if(i + 1 < n){
ans = max(ans, suf_max[i + 1] - pref[i] + a[i].second + a[i].first);
}
}
cout << ans << "\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
}
# | 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... |