Codeforce Round #214 Div2 C
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.
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.
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.
3 2
10 8 1
2 7 1
18
5 3
4 4 4 4 4
2 2 2 2 2
-1
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.
1 #include <cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <set> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <ctime> 10 #include <algorithm> 11 #include <queue> 12 13 using namespace std; 14 #define INF 0x7fffffff 15 #define maxm 1001 16 #define mp make_pair 17 #define pb push_back 18 #define rep(i,n) for(int i = 0; i < (n); i++) 19 #define re return 20 #define fi first 21 #define se second 22 #define sz(x) ((int) (x).size()) 23 #define all(x) (x).begin(), (x).end() 24 #define sqr(x) ((x) * (x)) 25 #define sqrt(x) sqrt(abs(x)) 26 #define y0 y3487465 27 //#define y1 y8687969 28 //#define fill(x,y) memset(x,y,sizeof(x)) 29 30 typedef vector<int> vi; 31 typedef long long ll; 32 typedef long double ld; 33 typedef double D; 34 typedef pair<int, int> ii; 35 typedef vector<ii> vii; 36 typedef vector<string> vs; 37 typedef vector<vi> vvi; 38 39 template<class T> T abs(T x) { re x > 0 ? x : -x; } 40 41 const int maxn = 115; 42 43 int n, m, t, s, k, x; 44 //string s; 45 int dp[maxn][260000]; 46 int a[maxn], b[maxn]; 47 int main(){ 48 m = 150000; 49 int mi = m-100000, ma = m+100000; 50 memset(dp[0], -2, sizeof dp[0]); 51 dp[0][m] = 0; 52 scanf("%d%d", &n, &k); 53 for (int i = 1; i <= n; i++)scanf("%d", &a[i]); 54 for (int i = 1; i <= n; i++){ 55 scanf("%d", &b[i]); b[i] = a[i] - k*b[i]; 56 for (int j = mi; j <= ma; j++){ 57 dp[i][j] = dp[i - 1][j]; 58 if (j - b[i] >= mi&&j - b[i] <= ma) 59 dp[i][j] = max(dp[i][j], dp[i - 1][j - b[i]] + a[i]); 60 } 61 } 62 printf("%d\n", dp[n][m] ? dp[n][m] : -1); 63 }
浙公网安备 33010602011771号