答案:<pre class="brush:cpp;toolbar:false">#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int f[1010], a[1010], v[1010];
int main() {
int t, o, i, j, k, n, m, maxn;
cin >> t;
for (o = 1; o <= t; o++) {
cin >> n >> k;
for (i = 1; i <= n; i++)
cin >> a[i];
for (i = 1; i <= n; i++)
cin >> v[i];
memset(f, 0, sizeof(f));
f[1] = v[1];
for (i = 2; i <= n; i++) {
maxn = 0;
for (j = 1; j < i; j++)
if (f[j] > maxn && a[i] - a[j] > k)
maxn = f[j];
f[i] = v[i] + maxn;
}
maxn = 0;
for (i = 1; i <= n; i++)
maxn = max(maxn, f[i]);
cout << maxn << endl;
}
return 0;
}</pre><p><span><span></span></span></p><pre class="brush:cpp;toolbar:false">//C语言实现的代码:
#include <stdio.h>
#include <stdbool.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
int maxProfit(int m[], int p[], int n, int k) {
int dp[100];
dp[0] = p[0];
for (int i = 1; i < n; i++) {
dp[i] = p[i];
for (int j = i - 1; j >= 0; j--) {
if (m[i] - m[j] > k) {
dp[i] = max(dp[i], dp[j] + p[i]);
}
}
}
int maxProfit = 0;
for (int i = 0; i < n; i++) {
maxProfit = max(maxProfit, dp[i]);
}
return maxProfit;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n, k;
scanf("%d %d", &n, &k);
int m[100];
int p[100];
for (int i = 0; i < n; i++) {
scanf("%d", &m[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
int result = maxProfit(m, p, n, k);
printf("%d\n", result);
}
return 0;
}</pre>
这道题暂未补解析。