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;
}

相关文章:

  • 2021-06-20
  • 2021-06-16
  • 2021-12-01
  • 2021-09-19
  • 2021-11-16
  • 2022-01-15
  • 2022-12-23
  • 2021-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-05
  • 2021-06-06
  • 2022-12-23
  • 2021-10-25
  • 2022-02-10
相关资源
相似解决方案