Cod sursa(job #173635)

Utilizator slayer4uVictor Popescu slayer4u Data 7 aprilie 2008 21:51:20
Problema Wanted Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 kb
#include <stdio.h>
#include <algorithm>
#include <math.h>

using namespace std;

long n, i, j, maxim, c[256][256][2];

struct lol
{
	long a, b;
};
lol x[256];

int cmpf(lol a, lol b)
{
	if (a.a != b.a)
		return a.a < b.a;
	return a.b < b.b;
}

void do_(long st, long dr, long dir)
{
	long &sol = c[st][dr][dir];

	if (c[st][dr][dir] != -1)
		return;

	if (st == dr)
	{
		c[st][dr][dir] = x[st].b;
		return;
	}

	if (st > dr)
	{
		c[st][dr][dir] = 0;
		return;
	}

	sol = 2147000000;
	if (!dir)
	{
		for (long i = st; i <= dr; ++i)
		{
			do_(st, i - 1, 0);
			do_(i + 1, dr, 1);
			long tmp1 = abs(x[dr].a - x[i].a) + 2 * x[i].b + (i > st) * abs(x[i].a - x[i - 1].a) + c[st][i - 1][0];
			long tmp2 = abs(x[dr].a - x[i].a) + 2 * x[i].b + (i < dr) * abs(x[i + 1].a - x[i].a) + c[i + 1][dr][1];
			sol = min(sol, max(tmp1, tmp2));
		}
	}
	else
	{
		for (long i = st; i <= dr; ++i)
		{
			do_(st, i - 1, 0);
			do_(i + 1, dr, 1);
			long tmp1 = abs(x[i].a - x[st].a) + 2 * x[i].b + (i > st) * abs(x[i].a - x[i - 1].a) + c[st][i - 1][0];
			long tmp2 = abs(x[i].a - x[st].a) + 2 * x[i].b + (i < dr) * abs(x[i + 1].a - x[i].a) + c[i + 1][dr][1];
			sol = min(sol, max(tmp1, tmp2));
		}
	}
}

int main()
{
	freopen ("wanted.in", "rt", stdin);
	freopen ("wanted.out", "wt", stdout);

	scanf("%ld", &n);

	for (i = 1; i <= n; ++i)
		scanf("%ld %ld", &x[i].a, &x[i].b);

	sort(x + 1, x + n + 1, cmpf);

	int sol = 2147000000;

	for (i = 1; i <= n; ++i)
		for (j = 1; j <= n; ++j)
			c[i][j][0] = c[i][j][1] = -1;

	for (i = 1; i <= n; ++i)
	{
		do_(1, i - 1, 0);
		do_(i + 1, n, 1);
		// 1: initial -> i -> i - 1 -> c[1][i - 1][0] 
		// 2: initial -> i -> i + 1 -> c[i + 1][n][1]
		long tmp1 = abs(x[i].a) + 2 * x[i].b + (i > 1) * abs(x[i].a - x[i - 1].a) + c[1][i - 1][0];
		long tmp2 = abs(x[i].a) + 2 * x[i].b + (i < n) * abs(x[i + 1].a - x[i].a) + c[i + 1][n][1];
		int tmp = max(tmp1, tmp2);
		sol = min(sol, tmp);
	}

	printf("%ld\n", sol);

	return 0;
}