Cod sursa(job #485918)

Utilizator Teodor94Teodor Plop Teodor94 Data 19 septembrie 2010 21:26:01
Problema Heavy metal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include<cstdio>
#include<algorithm>

using namespace std;

struct heavy
{
	int x,y;
};

const int N=100005;

heavy a[N];
int n,maxx,best[N];

bool comp(heavy a,heavy b)
{
	if (a.y<b.y)
		return true;
	if (a.y>b.y)
		return false;
	if (a.x<b.x)
		return true;
	return false;
}

int maxim(int x,int y)
{
	if (x<y)
		return y;
	return x;
}

void citire()
{
	scanf("%d",&n);
	for (int i=1;i<=n;++i)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		maxx=maxim(maxx,a[i].y);
	}
	sort(a+1,a+n+1,comp);
}

void rez()
{
	int j=1,aux;
	for (int i=1;i<=maxx;++i)
	{
		best[i]=best[i-1];
		while (i==a[j].y)
		{
			aux=best[a[j].x]+a[j].y-a[j].x;
			if (aux>best[i])
				best[i]=aux;
			++j;
		}
	}
	printf("%d\n",best[maxx]);
}

int main()
{
	freopen("heavymetal.in","r",stdin);
	freopen("heavymetal.out","w",stdout);
	citire();
	rez();
	return 0;
}