思路:按b值从小到大排序,然后就是简单DP了,因为值越大的肯定放后面转移,所以是可行的
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 255;
int t, n, m;
struct Tree {
int a, b;
} tree[N];
bool cmp(Tree a, Tree b) {
return a.b < b.b;
}
int dp[N][N];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &tree[i].a);
for (int i = 1; i <= n; i++)
scanf("%d", &tree[i].b);
sort(tree + 1, tree + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + tree[i].a + tree[i].b * (j - 1));
}
}
printf("%d\n", dp[n][m]);
}
return 0;
}