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 MAXN 5001
long long n;
vector<pair<long long,long long>> vec;
long long pref[MAXN];
map<long long,long long> poslednji;
map<long long,long long> prvi;
int main()
{
cin>>n;
vec.push_back({0,0});
for (long long i=1;i<=n;i++)
{
long long a,b;cin>>a>>b;
vec.push_back({a,b});
}
sort(vec.begin(),vec.end());
pref[0]=0;
for (long long i=1;i<=n;i++) pref[i]=pref[i-1]+vec[i].second;
for (long long i=1;i<=n;i++)
{
poslednji[vec[i].first]=i;
if (prvi.find(vec[i].first)==prvi.end()) prvi[vec[i].first]=i;
}
long long ans=pref[poslednji[vec[1].first]];
long long trenmaks=vec[1].first;
for (auto&p:poslednji)
{
if (p.first==vec[1].first) continue;
long long desno=p.second,a3=p.first;
long long levo=prvi[a3];
long long s=pref[desno]-pref[levo-1];
ans=max(ans,s);
long long i=levo-1;
long long val=s-a3+pref[i]+trenmaks;
ans=max(ans,val);
trenmaks=max(trenmaks,a3-pref[i]);
}
cout<<ans<<endl;
}
# | 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... |