Pagini recente » Cod sursa (job #2268838) | Cod sursa (job #151808) | Cod sursa (job #1901741) | Cod sursa (job #136098) | Cod sursa (job #1601061)
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("heavymetal.in");
ofstream g("heavymetal.out");
int i,n,j,best[100000];
struct formatie{int a,b;}v[100000];
bool compare(formatie x,formatie y)
{
if(x.b==y.b) return x.a<y.a;
return x.b<y.b;
}
int main()
{
f>>n;
for(i=1;i<=n;i++) f>>v[i].a>>v[i].b;
sort(v+1,v+n+1,compare);
for(i=1;i<=n;i++)
{
if(v[i].b-v[i].a>best[i]) best[i]=v[i].b-v[i].a;
if(best[i-1]>best[i]) best[i]=best[i-1];
j=i;
while(v[i].b>v[j].a&& j<=n) j++;
while(j<=n){if(best[i]+v[j].b-v[j].a>best[j]) best[j]=best[i]+v[j].b-v[j].a;j++;}
}
g<<best[n];
f.close();
g.close();
return 0;
}