Codeforces 366C Dima and Salad 【dp】

题目链接:Codeforces 366C Dima and Salad
C. Dima and Salad
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Dima, Inna and Seryozha have gathered in a room. That’s right, someone’s got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something.

Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words, , where aj is the taste of the j-th chosen fruit and bj is its calories.

Inna hasn’t chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem — now the happiness of a young couple is in your hands!

Inna loves Dima very much so she wants to make the salad from at least one fruit.

Input
The first line of the input contains two integers n, k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10). The second line of the input contains n integers a1, a2, …, an (1 ≤ ai ≤ 100) — the fruits’ tastes. The third line of the input contains n integers b1, b2, …, bn (1 ≤ bi ≤ 100) — the fruits’ calories. Fruit number i has taste ai and calories bi.

Output
If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer — the maximum possible sum of the taste values of the chosen fruits.

Examples
input
3 2
10 8 1
2 7 1
output
18
input
5 3
4 4 4 4 4
2 2 2 2 2
output
-1
Note
In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition fulfills, that’s exactly what Inna wants.

In the second test sample we cannot choose the fruits so as to follow Inna’s principle.

题意:从n个物品中选m个物品,要求 mi=1a[i]=kmi=1b[j] ,问最大的 mi=1a[i]

思路:转化问题,用容量为0的背包去装n个物品,物品重量为 a[i]b[i] 且价值为 a[i] ,要求装满背包且总价值最大。
dp[i][j] 表示前i个物品,总重量为j的最大价值。
dp[i][j]=max(dp[i1][j],dp[i1][ja[i]+kb[i]]+a[i])
由于j可以为负数,我们扩容M,求出的 dp[n][M] 就是结果。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e4 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
pii a[110];
int dp[110][201000];
int N;
int main()
{
    int n, k; cin >> n >> k;
    for(int i = 1; i <= n; i++) {
        cin >> a[i].se;
    }
    N = n * 1000;
    for(int i = 1; i <= n; i++) {
        int b; cin >> b;
        a[i].fi = a[i].se - k * b;
    }
    CLR(dp, -INF); dp[0][N] = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 2*N; j >= a[i].fi; j--) {
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-a[i].fi] + a[i].se);
        }
    }
    if(dp[n][N] <= 0) {
        cout << -1 << endl;
    }
    else {
        cout << dp[n][N] << endl;
    }
    return 0;
}

你可能感兴趣的:(Codeforces 366C Dima and Salad 【dp】)