Pagini recente » Cod sursa (job #1882801) | Cod sursa (job #2931725) | Cod sursa (job #1983519) | Cod sursa (job #1267349) | Cod sursa (job #2575630)
#include <bits/stdc++.h>
using namespace std;
ifstream in("heavymetal.in");
ofstream out("heavymetal.out");
int n,dp[100005],j;
struct timp
{
int r,l;
}v[100005];
bool comp(timp x, timp y)
{
return (x.l<y.l or (x.l==y.l and x.r<y.r));
}
int main()
{
in>>n;
for(int i=1;i<=n;i++)
{
in>>v[i].r>>v[i].l;
}
sort(v+1,v+n+1,comp);
dp[1]=v[1].l-v[1].r;
for(int i=2;i<=n;i++)
{
int st,dr;
st=1;dr=i-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(v[mij].l<=v[i].r)
{
j=mij;st=mij+1;
}
else dr=mij-1;
}
dp[i]=max(dp[i-1],dp[j]+v[i].l-v[i].r);
}
out<<dp[n];
}