一本通 1296:开餐馆
开餐馆
01背包的变式
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define M 1000000
int f[M];
int t, n, k, ans;
int w[M], c[M];
int main() {
scanf("%d", &t);
while(t--) {
memset(f, 0, sizeof(f));
ans = 0;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
for(int i = 1; i <= n; i++) scanf("%d", &c[i]), f[i] = c[i];
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
if(w[i] - w[j] > k) f[i] = max(f[i], f[j] + c[i]);
ans = max(f[i], ans);
}
}
printf("%d\n", ans);
}
return 0;
}
唯愿,青春不辜负梦想,未来星辰闪耀