Gangsters

Description

N gangsters are going to a restaurant. The i-th gangster comes at the time Ti and has the prosperity Pi. The door of the restaurant has K+1 states of openness expressed by the integers in the range [0, K]. The state of openness can change by one in one unit of time; i.e. it either opens by one, closes by one or remains the same. At the initial moment of time the door is closed (state 0). The i-th gangster enters the restaurant only if the door is opened specially for him, i.e. when the state of openness coincides with his stoutness Si. If at the moment of time when the gangster comes to the restaurant the state of openness is not equal to his stoutness, then the gangster goes away and never returns. 
The restaurant works in the interval of time [0, T]. 
The goal is to gather the gangsters with the maximal total prosperity in the restaurant by opening and closing the door appropriately. 

Input

?The first line of the input file contains the values N, K, and T, separated by spaces. (1 <= N <= 100 ,1 <= K <= 100 ,0 <= T <= 30000 ) 
?The second line of the input file contains the moments of time when gangsters come to the restaurant T1, T2, ..., TN, separated by spaces. ( 0 <= Ti <= T for i = 1, 2, ..., N) 
?The third line of the input file contains the values of the prosperity of gangsters P1, P2, ..., PN, separated by spaces. ( 0 <= Pi <= 300 for i = 1, 2, ..., N) 
?The forth line of the input file contains the values of the stoutness of gangsters S1, S2, ..., SN, separated by spaces. ( 1 <= Si <= K for i = 1, 2, ..., N) 
All values in the input file are integers. 

Output

Print to the output file the single integer ?the maximal sum of prosperity of gangsters in the restaurant. In case when no gangster can enter the restaurant the output should be 0.

Sample Input

4 10 20
10 16 8 16
10 11 15 1
10 7 1 8

Sample Output

26
  
f[i]表示第i个人进入后的最大繁荣度,如果j进入后i还可以进的话(t[i] - t[j] > abs(s[j] - s[i]))
f[i] = max(f[i], f[j] + p[i])
注意要按时间从小到大给排个序,还要注意由于最开始门是关着的所以要加上第0个人s,t,p都为零且强制进店!
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

const long N = 110;
struct ty
{
	long t, s, p;
}a[N];
long n, k, T;
long f[N];

long max(long a, long b)
{
	return a > b ? a : b;
}
bool comp(ty a, ty b)
{
	if (a.t < b.t) return 1;
	return 0;
}
int main()
{
	freopen("poj1036.in", "r", stdin);
	scanf("%d%d%d", &n, &k, &T);
	for (long i = 1; i <= n; i++)
		scanf("%d", &a[i].t);
	for (long i = 1; i <= n; i++)
		scanf("%d", &a[i].p);
	for (long i = 1; i <= n; i++)
		scanf("%d", &a[i].s);

    sort(a + 1, a + n + 1, comp);

	memset(f, 128, sizeof(f));
	f[0] = 0;
	a[0].t = 0; a[0].s = 0;
	for (long i = 1; i <= n; i++)
	{
		for (long j = 0; j < i; j++)
		{
			long time = a[i].t - a[j].t;
			long ds = abs(double(a[i].s - a[j].s));
			//cout << i << ' ' << j << ' ' << time << ' ' << ds << endl;
			if (time >= ds)
			{
				f[i] = max(f[i], f[j] + a[i].p);
			}
		}
	}
	long maxn = 0;
	for (long i = 1; i <= n; i++)
	{
		if (f[i] > maxn)
			maxn = f[i];
	}
	cout << maxn <<endl;
    return 0;
}