Topic Link:
Codeforces 366C topic to the effect:
Give n items, there are two attributes, ask the last property is the sum of the second attribute of K times, the first property is the maximum number. Topic Analysis: We will make a deformation, weight for a[i]-b[i]*k, the benefit is a, then we only need to do the weight of the backpack, the quality of negative to take absolute value to do a backpack, and then the weight of the same backpack benefits and is the current weight of the maximum income.
AC Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 107
using namespace std;
int dp [MAX * MAX], dd [MAX * MAX];
int n, k, a [MAX], b [MAX], c [MAX];
int main ()
{
while (~ scanf ("% d% d", & n, & k))
{
for (int i = 0; i <n; i ++)
scanf ("% d", & a [i]);
for (int i = 0; i <n; i ++)
scanf ("% d", & b [i]);
for (int i = 0; i <n; i ++)
c [i] = a [i] -b [i] * k;
memset (dp, -0x3f, sizeof (dp));
memset (dd, -0x3f, sizeof (dd));
dp [0] = dd [0] = 0;
for (int i = 0; i <n; i ++)
if (c [i]> = 0)
for (int j = 10000; j> = c [i]; j--)
dp [j] = max (dp [j-c [i]] + a [i], dp [j]);
for (int i = 0; i <n; i ++)
if (c [i] <0)
{
c [i] = -c [i];
for (int j = 10000; j> = c [i]; j--)
dd [j] = max (dd [j-c [i]] + a [i], dd [j]);
}
int ans = -1;
for (int i = 0; i <= 10000; i ++)
{
if (dd [i] == 0 && dp [i] == 0)
continue;
ans = max (ans, dp [i] + dd [i]);
}
printf ("% d \ n", ans);
}
}