AcWing 1025. 开餐馆 Java版
原题链接
简单
作者:
辣条_9
,
2024-12-10 18:50:44
,
所有人可见
,
阅读 1
import java.util.*;
public class Main {
static int N = 110;
static int [] m = new int [N],p = new int[N];
static int [] dp = new int [N];// f[i]从前i号点中选的最大值
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
// 初始化
Arrays.fill(m, 0);
Arrays.fill(p, 0);
Arrays.fill(dp, 0);
int n = sc.nextInt();int k = sc.nextInt();
for (int i = 1; i <= n; i++) m[i] = sc.nextInt();
for (int i = 1; i <= n; i++) p[i] = sc.nextInt();
for(int i = 1;i <= n;i++){
dp[i] = Math.max(dp[i-1], p[i]);
// 餐厅距离大于k的时候,可以选择前面的餐厅
for(int j = 1;j < i;j++){
if (m[j] + k < m[i]) {
dp[i] = Math.max(dp[i],dp[j]+p[i]);
}else {
break;
}
}
}
System.out.println(dp[n]);
}
}
}