Pagini recente » Cod sursa (job #899499) | Cod sursa (job #499351) | Cod sursa (job #2675187) | Cod sursa (job #1661161) | Cod sursa (job #2346571)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
const int Maxx=1e5+1;
struct show{
int st,fs;
}A[Maxx];
bool compx(show a,show b){
if (a.fs==b.fs){
return a.st<b.st;
}
return a.fs<b.fs;
}
ifstream fin("heavymetal.in");
ofstream fout("heavymetal.out");
int n;
int dp[Maxx];
//dp[i] - maximul ce a fost vazut pana la spectacolul i in sirul ordonat
int bs(int val,int dr){
int st=1,mid,ret=0;
while(st<=dr){
mid=(st+dr)>>1;
if (A[mid].fs<=val){
st=mid+1;
} else {
dr=mid-1;
}
}
return dr;
}
int main() {
int i,next;
fin>>n;
for (i=1;i<=n;++i){
fin>>A[i].st>>A[i].fs;
}
sort(A+1,A+n+1,compx);
dp[1]=A[1].fs-A[1].st;
for (i=2;i<=n;++i){
next=bs(A[i].st,i-1);
//cout<<next<<"\n";
dp[i]=max(dp[i-1],dp[next]+A[i].fs-A[i].st);
}
cout<<dp[n];
return 0;
}