C. Dima and Salad

    xiaoxiao2021-03-25  171

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input output standard 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 nk (1 ≤ n ≤ 100, 1 ≤ k ≤ 10). The second line of the input contains n integersa1, 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个水果每个水果都有价值和卡路里,要求你选的水果的价值之和除以卡路里等于k,且保证水果的价值最高

    解题思路:一开始想通过递归写,发现超时,最后讲题大神说用的是dp,然后把价值除以卡路里等于k变成一个一边为0的式子表示价值,然后用01背包去做,dp[i][j]指的前i个水果的价值,然后转化就行了,其实DP我基本不会,不太会绕这个转化方程,也是跟着其他人的代码看着学的,也是第一道DP,留着以后查询用

    #include<iostream> #include<cstdio> #include<stdio.h> #include<cstring> #include<cstdio> #include<climits> #include<cmath> #include<vector> #include <bitset> #include<algorithm> #include <queue> #include<map> using namespace std; long long int dp[105][300005],a[105],b[105],c[105],n,m,i,j; int main() { cin >> n >> m; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= n; i++) { cin >> b[i]; } for (i = 1; i <= n; i++) { c[i] = a[i] - b[i] * m; } for (i =0; i <= n; i++) { for (j = 0; j <= 300005; j++) { dp[i][j] = -99999999; } } dp[0][200000] = 0; for (i = 1; i <= n; i++) { for (j = -200000; j <= 200000; j++) { dp[i][j + 200000] = max(dp[i][j + 200000], max(dp[i - 1][j + 200000], dp[i - 1][j + 200000 - c[i]]+a[i])); } } if (dp[n][200000] != 0) { cout << dp[n][200000] << endl; } else { cout << -1 << endl; return 0; } }
    转载请注明原文地址: https://ju.6miu.com/read-7551.html

    最新回复(0)