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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given n fruits, each with taste a_i and calories b_i. Inna wants to choose a non-empty subset of fruits such that (sum a_i) / (sum b_i) = k, i.e., sum a_i = k * sum b_i. This is equivalent to sum (a_i - k * b_i) = 0. Let c_i = a_i - k * b_i. Then we need subset with total c = 0, and maximize sum a_i (or equivalently sum a_i). Since a_i are positive, maximizing sum a_i among subsets with total c=0.
But note: c_i can be positive, negative, zero. We need to pick at least one fruit. The problem: n <= 100, a_i, b_i <= 100, k <= 10. So values: a_i up to 100, b_i up to 100, k up to 10 => c_i = a_i - kb_i ranges from 1 - 10100 = -999 to 100 - 0 = 100. Actually b_i >=1, so min c_i is 1 - 10100 = -999; max c_i is 100 - 11? Actually k min 1, so max is 100 - 11? b_i min 1, so max c_i = 100 - 11 = 99, but if b_i = 1 and k small? For k=10, max c_i might be 100 - 101 = 90. So c_i range roughly -1000 to 100. But n is only 100, so total sum of positive c_i or negative sum limited: sum of positive c_i at most 100 * 100 = 10000? Actually max positive c_i maybe 100, but actually a_i <=100, k>=1, so c_i positive only if a_i > k b_i, which can't be too big; if b_i small, say b_i=1, a_i=100, k=1 => c=99; k=10 => c=90. So max positive sum ~100100=10000. Negative sum min could be -100 * (k*b_i - a_i) worst-case b_i=100, a_i=1, k=10 => c = 1-1000 = -999, sum could be -99900 ~ -100k. So range is about -100000 to 10000. However we can shift index for DP.
We want to maximize sum a_i subject to sum c_i = 0. This is a classic knapsack-like problem: we can treat each fruit as item with value a_i and weight c_i, need total weight = 0, maximize value. Since c_i can be negative, we can have two DP arrays: dp[pos] = maximum total taste achievable with total c = pos, where pos can be negative. We can offset by a constant SHIFT to make index nonnegative.
Alternatively, we can do DP over the sum of positive c_i or negative. Because we need sum = 0, we could consider DP over the difference.
Let’s define offset = sum of absolute negative c_i? Actually we can compute the maximum possible positive sum and minimum possible negative sum. Let total_possible_range = sum_{i} |c_i| maybe. But n=100, each |c_i| max ~999, so total max ~ 100000. That's acceptable for DP if we use O(n * range) with range ~200001? Actually max sum of positive c_i ≤ 100100 = 10000? Wait is that correct? Let's compute worst-case positive c_i: a_i up to 100, b_i min 1, k min 1 => c_i = a_i - b_i. Maximum positive when a_i=100, b_i=1 => 99. So positive sum max ≤ 10099 = 9900. But if k is small, say k=1, b_i could be 1, a_i=100 => 99. If k is larger, positive may be smaller. So positive sum max < 10000. Negative sum min: a_i min 1, b_i max 100, k max 10 => c_i = 1 - 1000 = -999, sum min = -99900, about -100k. So total range from -100k to +10k. That's about 110k length. That's acceptable for DP with O(n*range) = 100 * 110k = 11 million, which is fine.
But we must be careful: DP for maximum taste; we can store -inf for impossible states. We'll use offset = maybe total_neg_sum (absolute value of min possible sum). Let's compute min_sum = sum of all negative c_i? Actually we can compute lower_bound = sum of all c_i where c_i negative? But we need the min possible total when selecting subset. The min total is the sum of the most negative selection? Actually we can select any subset, so the minimum total c achievable is the sum of all c_i that are negative? Wait we can choose any subset, so the total c can be from sum of all negative c_i (if we take all negative items only) to sum of all positive c_i (if we take all positive items only). But we might also take some positive and negative together; but the overall min might be lower if we take only negative items? Actually if we take items with negative c_i, total negative; positive items increase total. So the minimum achievable total is sum of all c_i that are negative (if we take all negative items and no positive). But could it be even lower? No, because if we also include some positive items, total increases. So the minimum is the sum of all negative c_i (i.e., sum of all items with c_i < 0). Similarly, maximum is sum of all positive c_i. But note that there might be items with c_i=0; they don't affect total but add taste. They can always be added arbitrarily because they don't change the sum condition but increase taste, so we should always include all items with c_i=0 because they only increase taste without violating sum=0 condition. So we should definitely take all fruits with c_i=0. However careful: Inna must choose at least one fruit. Including zero c_i items is fine. But if there are no zero-sum items and we find a subset with c=0 that includes other items, that's fine.
Thus we can pre-add all items with c_i=0 to the answer (they contribute taste) and then remove them from consideration because they don't affect the constraint. But careful: If we include zero c_i items, they increase taste but do not affect the equation. So any optimal solution can include all zero c_i items because adding them doesn't break the ratio condition (since sum a increases, sum b increases proportionally? Actually check: If we add an item with c_i=0, i.e., a_i = k b_i, then the ratio of added part is exactly k, so overall ratio remains k if original ratio was exactly k. Yes, because if we have a set S with sum a_S = k sum b_S, and we add an item i with a_i = k b_i, then new sum a = k sum b_S + k b_i = k (sum b_S + b_i), so ratio remains k. So we can always include any zero c_i items. So indeed they can be taken greedily. So we can add all a_i for which a_i - k b_i = 0 to the base taste and then ignore those items in the DP.
Now the remaining items have c_i != 0: some positive, some negative. We need to select a subset (possibly empty? but at least one fruit overall; but we already have maybe some zero items, if there is at least one zero item then we already have non-empty subset overall even if we pick no other items. But the problem says at least one fruit; if we have zero items, we need to select at least one from the non-zero items. But we might also consider that if we pick no items from non-zero, and we had zero items, that's fine. But if there are no zero items, we must select at least one non-zero item in our DP. So we need to enforce that the chosen subset from non-zero items is non-empty if the zero-items set is empty.
But we can handle that by checking final DP state; if base_taste > 0 (i.e., we have at least one zero item) then we can have DP result for subset sum 0 with possibly empty subset? Actually if we include zero items, we already have some fruits. So even if we pick no other fruits, we have a valid salad (since zero items satisfy condition by themselves? Check: If we pick only zero items, then sum a = k sum b, so condition satisfied. So yes. So if there is at least one zero-item, the answer is base_taste + maximum taste from other items that we can add while keeping sum c = 0, but we can also choose not to add any, which gives base_taste. However the DP can also consider empty subset giving 0 additional taste. So we need to take max of base_taste and base_taste + max additional taste from DP with sum=0. But careful: If there are zero items, we must include them all (since they can be added without cost) but it's not mandatory? Actually we can choose any subset, but we want maximum total taste. Since zero items contribute positive taste and do not affect ratio, including them always increases taste, so any optimal solution will include all zero-items. So we can assume they are always included. So base_taste = sum of a_i for c_i=0. Then we need to decide which of the remaining items to include to maximize total taste subject to sum c_i = 0. This is a classic subset sum problem with both positive and negative weights, maximizing total taste. Since base_taste is fixed, we can focus on additional items.
Thus the problem reduces to: Given items with weight c_i (non-zero) and value a_i, find a subset (possibly empty) such that sum weight = 0, maximizing sum value. If we cannot achieve sum weight = 0 with any subset (including empty? Empty set sum weight = 0, but empty set corresponds to picking no additional items. However is empty set allowed? Yes, if we have at least one fruit from zero items, then we can have empty additional set. But if there are no zero items, then we cannot pick empty set because then we would have no fruits at all; the problem requires at least one fruit. So we need to handle the case where base_taste = 0 separately: then we must select a non-empty subset with sum c = 0. So DP must consider non-empty condition.
Thus approach: compute c_i = a_i - k * b_i. Separate zero items: sum_zero_taste = sum a_i for which c_i = 0. Let the other items be list L with c_i != 0.
If sum_zero_taste > 0, then we already have a valid solution (all zero items). So we can set answer = sum_zero_taste + best additional taste from L where sum c = 0, where additional can be empty. If no subset from L yields sum c = 0, we can still have answer = sum_zero_taste (from zero items only). So answer is at least sum_zero_taste. But we must also consider if there is a better solution that excludes some zero items? That would be suboptimal because zero items only add positive taste with no penalty. So including all zero items is optimal. So fine.
If sum_zero_taste = 0, then we need to find a non-empty subset from all items (since no zero items) such that sum c = 0 and maximize sum a. If no such subset exists, answer = -1.
Thus we need to compute for items with non-zero c the maximum sum a with sum c = 0, possibly forcing non-empty.
Now DP: We'll define DP over possible total c values. Since c_i can be negative and positive, we need offset. Determine max positive sum P = sum_{i: c_i>0} c_i. Determine min negative sum N = sum_{i: c_i<0} c_i (negative number). Then total range = [N, P]. We'll set offset = -N (or maybe a fixed constant like 10000). Actually N is negative, so offset = -N to shift to nonnegative indices. Let SHIFT = -N (which is positive). Then index for total c = t is pos = t + SHIFT. The DP size = SHIFT + P + 1.
We need to store maximum taste for each total c. Initialize dp with -inf (or -1 to indicate unreachable). dp[SHIFT] = 0 (empty subset). Then for each item with weight w = c_i and value v = a_i, we update. Since we have both positive and negative weights, we need to do either forward or backward depending on sign. For positive w, we should iterate from high to low (like knapsack) to avoid reuse? Actually standard knapsack for unbounded? But each item can be used at most once. For w positive, to avoid using same item multiple times, we iterate from max index down to min index: for pos from max_pos down to min_pos such that dp[pos - w] is reachable then update dp[pos] = max(dp[pos], dp[pos - w] + v). For w negative, we need to iterate from low to high because subtracting a negative is adding? Let's derive: We have dp array; for each item with weight w (could be negative). We want new_dp[t] = max(old_dp[t], old_dp[t - w] + v) if t-w in range. Because if we include the item, the previous total should be t - w. So if w is negative, t - w = t - (negative) = t + |w|, which is larger than t. So when processing negative w, to avoid using item multiple times, we need to iterate t from low to high (because the dependency is on larger t). So we can handle by condition: if w >= 0, iterate descending; else ascending. That's typical for 0/1 knapsack with negative weights.
Alternatively, we can use two-dimensional DP or store next array each time to avoid order issues: for each item, create new dp2 initially copy of dp, then for each state t where dp[t] reachable, update dp2[t + w] = max(dp2[t + w], dp[t] + v). That's easier and avoids confusion about iteration direction. Since n <= 100 and range <= 110k, O(n * range) with copying array each time might be okay? But copying array of size ~110k 100 times = 11 million assignments, fine. But we also need loops. However we might also just use one array with careful ordering using two loops based on sign, but copying might be simpler.
Let's compute approximate range: N = sum of negative c_i, P = sum of positive c_i. Worst-case negative sum: each item could be as low as -999, but note that if b_i=100, a_i=1, k=10 => -999. But also some items may be positive. However maximum absolute negative sum could be up to ~100999 = 99,900. Positive sum up to ~10099=9,900. So total range length = P - N = about 99,900 + 9,900 = 109,800. So SHIFT = -N up to 99,900. So DP size ~110k. That's fine.
Now after processing all items, we have dp[SHIFT] corresponds to total c=0. This includes the empty subset. If we have zero items, we need a non-empty subset. So we must exclude the empty subset. How to enforce non-empty? We can either subtract the empty case: if dp[SHIFT] is 0 from empty, we need to check if there is any other subset (non-empty) also achieving total c=0. That could be found by storing additional information: number of items? Or we can do DP where we initialize dp[SHIFT] = 0 but we treat that as empty. After DP, if dp[SHIFT] > 0, that means we have a non-empty subset? But note that a_i are positive (>=1), so any non-empty subset yields sum a >= 1. So if dp[SHIFT] > 0, then it must correspond to a non-empty subset (since empty gives 0). However it's possible that dp[SHIFT] could be 0 from a non-empty subset? No, because a_i are positive, so any non-empty subset has positive taste. So dp[SHIFT] > 0 indicates a non-empty subset. But could dp[SHIFT] be 0 from empty only? Yes. So after DP, if we have base_taste=0, we need to check if dp[SHIFT] > 0. If so, answer = sum_zero_taste (0) + dp[SHIFT]. If not, answer = -1. However careful: Could there be a case where dp[SHIFT] = 0 but there exists a subset with zero total c that yields zero taste? That would require all items in that subset to have a_i=0, but a_i are >=1, so impossible. So dp[SHIFT] > 0 iff there is a non-empty subset with total c=0. Good.
If base_taste>0, we can add dp[SHIFT] (which may be 0 from empty) to base_taste to get total taste. But we also might consider not including any zero items? That would be suboptimal because zero items only add taste without affecting sum. So answer = base_taste + dp[SHIFT] (where dp[SHIFT] is max additional taste from other items with sum c=0, possibly empty). However note: It might be optimal to not include all zero items? No, as argued.
But wait: Could including zero items cause some DP solution to become invalid? No, because zero items don't affect the sum c condition. They just add taste. So if we include them, we can still add any subset from non-zero items that sums to zero. So the maximum total taste = base_taste + max additional taste from non-zero items with sum c=0. So that's correct.
Edge Cases: What if there are no non-zero items? Then dp[SHIFT] = 0 from empty. If base_taste >0, answer = base_taste. If base_taste=0, then no fruits? But n >=1, so if all c_i=0? Actually if all c_i=0, then base_taste = sum of all a_i > 0, so answer is that sum. That's fine.
Now we need to compute DP. Let's define items list with w_i = c_i, v_i = a_i. We'll compute total_negative = sum of negative w_i (which is negative) and total_positive = sum of positive w_i. SHIFT = -total_negative. Let size = total_positive - total_negative + 1 (since inclusive). Actually indices from 0 to total_positive - total_negative (i.e., SHIFT+total_positive). We'll use vector dp(size, -INF). dp[SHIFT] = 0.
For each item (w,v):
If w == 0, already handled separately. So w != 0.
We'll create new dp_next = dp (copy). Then for each state s (0..size-1) where dp[s] != -INF, compute ns = s + w. If ns within 0..size-1, then dp_next[ns] = max(dp_next[ns], dp[s] + v). After processing all items, dp = dp_next.
This is O(n * range) with copying, which is fine.
But we could also do in-place with direction based on sign to save memory and time; but copying also fine. However careful: When we iterate over all s, and update dp_next, we should ensure we don't use the same item multiple times. Because we are using original dp for all updates, then assigning to dp_next, that's fine. So simple.
But we need to ensure we don't exceed array bounds: w can be positive up to maybe 99, negative down to -999. So we need to compute total_negative and total_positive accurately. However those sums depend on which items we consider; we have the list L. Compute sum_neg = 0, sum_pos = 0. For each item, if w>0: sum_pos += w; else sum_neg += w (which is negative). Then total_negative = sum_neg (negative), total_positive = sum_pos. Then SHIFT = -sum_neg. Size = sum_pos - sum_neg + 1. Since sum_neg negative, -sum_neg positive, and sum_pos positive, size = sum_pos + (-sum_neg) + 1 = sum_pos - sum_neg + 1.
Edge: If there are no items? Then sum_pos = sum_neg = 0, size = 1. dp[0] = 0. That's fine.
But wait: In DP we will have indices from 0 to size-1. The state representing total c = t corresponds to index = t + SHIFT. That works for t in [sum_neg, sum_pos]. Since we only consider items from L, the possible totals from subsets will be within that range.
Implementation steps:
Read n, k.
Read arrays a[1..n], b[1..n].
Compute c_i = a_i - k * b_i.
Initialize total_taste = 0 (for zero c_i). Also create vectors for items: list of pairs (c_i, a_i) where c_i != 0.
For each i: if c_i == 0: total_taste += a_i; else: items.push_back({c_i, a_i}).
If items empty: then answer = total_taste if total_taste > 0 else -1 (but n>=1, so if total_taste>0 then okay; if total_taste==0 it means all a_i=0? But a_i>=1, so total_taste>0 if any c_i=0. Actually if items empty and total_taste ==0, that would imply n=0? But n>=1 and a_i>=1, so total_taste must be >0 because if all c_i !=0 then items not empty, but if items empty means all c_i=0, then total_taste = sum a_i >0. So answer = total_taste. So fine.
Else we need DP for items.
Compute sum_neg = 0, sum_pos = 0 for items.
For each item (w, v):
if w > 0: sum_pos += w;
else: sum_neg += w; // w negative
SHIFT = -sum_neg.
size = sum_pos - sum_neg + 1.
Initialize dp with vector(size, -INF). Use maybe -1e9 or something.
dp[SHIFT] = 0.
For each item (w, v):
vector new_dp = dp;
for (int s = 0; s < size; ++s) {
if (dp[s] == -INF) continue;
int ns = s + w;
if (ns >= 0 && ns < size) {
if (new_dp[ns] < dp[s] + v) {
new_dp[ns] = dp[s] + v;
}
}
}
dp = move(new_dp);
After loop, best = dp[SHIFT] (which is max taste from items for total c=0). Note: this includes empty subset (taste=0).
If total_taste > 0:
answer = total_taste + best; // best could be 0
else: // total_taste == 0, meaning no zero items, so we need non-empty subset.
if best > 0: answer = best;
else answer = -1;
Output answer.
Check: Is it always optimal to take all zero items? Suppose there is a zero item and also some other items that together sum to zero, but maybe including zero items prevents some combination? No, because zero items do not affect sum c condition, so any subset of non-zero items that works with zero items is also valid without zero items; and adding zero items only increases taste. So it's optimal.
But what about the case where there are zero items but also we might want to not include some zero items to maybe allow a different combination? No, zero items are independent: they only add taste and don't affect the equation. So always include all.
Now test with given examples:
Example 1:
n=3,k=2
a=[10,8,1], b=[2,7,1]
c = [10-4=6, 8-14=-6, 1-2=-1]? Wait compute: k=2, so c1 = 10-22=10-4=6; c2 = 8-27=8-14=-6; c3 = 1-2*1=1-2=-1. So zero items? none. So total_taste=0.
Items: (6,10), (-6,8), (-1,1)
sum_neg = -6 + -1 = -7, sum_pos = 6 => SHIFT = 7, size = 6 - (-7) + 1 = 14? Actually -(-7) =7, so size = 6+7+1=14.
DP: start dp[7] =0.
Process item1 (6,10): from s=7, ns=13, new_dp[13]=10.
Process item2 (-6,8): from s=7 -> ns=1, new_dp[1]=8; from s=13 -> ns=7, new_dp[7] = max(0,10+8=18) = 18.
Process item3 (-1,1): updates: from s=7->ns=6, 1; from s=13->ns=12, 11; from s=1->ns=0,9; from s=7? Actually careful after processing item2, dp[7]=18? Let's simulate: after item2, dp[7] becomes 18? Let's compute step by step:
After item1: dp[7]=0, dp[13]=10.
For item2 (-6,8): start new_dp = dp. For each s:
s=7 (dp=0): ns=1 => new_dp[1]=max(0,8)=8.
s=13 (dp=10): ns=7 => new_dp[7]=max(0,10+8=18)=18.
Others unchanged.
So dp now: dp[7]=18, dp[13]=10, dp[1]=8.
For item3 (-1,1): start new_dp=dp.
s=7 (18) -> ns=6 => new_dp[6]=max(?, 19)=19.
s=13 (10) -> ns=12 => new_dp[12]=11.
s=1 (8) -> ns=0 => new_dp[0]=9.
also s=? maybe other states? dp[?] others -inf.
Also we need to consider s=6? initially -inf, etc.
So after processing, dp[7] remains 18 (unchanged). dp[6]=19, dp[12]=11, dp[0]=9.
So best = dp[SHIFT]=dp[7]=18. best>0 => answer=18. Matches.
Example 2:
n=5,k=3
a=[4,4,4,4,4] b=[2,2,2,2,2]
c = 4 - 3*2 = 4-6 = -2 each. So all c_i = -2, no zero. items: all (-2,4). sum_neg = -10, sum_pos = 0 => SHIFT = 10, size = 0 - (-10) +1 = 11.
DP: start dp[10]=0.
Process each item (-2,4). We need to find subset sum = 0 i.e., total c =0 => index = 0+10=10. We need to select some items with total c = 0. Each contributes -2, so to sum to 0 we need an equal number of items that sum to 0? Actually sum of negative numbers cannot be zero unless we have zero items. So no non-empty subset can sum to 0. Empty subset sums to 0, but that gives taste 0. DP will only have states that are multiples of -2. dp[10]=0; after first item, from s=10 -> ns=8 => dp[8]=4; after second, from s=10 ->8? Actually need to simulate but eventually dp[10] might get updated? Let's see: Starting dp[10]=0. After processing one item (-2,4): dp[8]=4. After second: from s=10->ns=8? Actually from s=10 (0) -> ns=8 => try update dp[8] = max(prev, 0+4=4) same; from s=8 (4) -> ns=6 => dp[6]=8. So dp[10] remains 0. After third: from s=10 -> ns=8; from s=8->ns=6; from s=6->ns=4 => dp[4]=12. So dp[10] remains 0. After all, dp[10]=0. So best=0, total_taste=0 => answer -1. Matches.
Now consider case with zero items and also non-zero: e.g., n=1, k=1, a=[5], b=[5] => c=0, zero item. total_taste=5, items empty, answer=5.
Case where zero items present but also non-zero items can achieve sum=0, DP gives best>0: answer base_taste+best.
Edge Cases: Possibly there are both zero items and non-zero items, and the DP best=0 (i.e., empty set only). Then answer = base_taste, which is valid.
Now, what about if there are zero items but also we might get a better solution by not including all zero items? Not possible because adding zero items always increases taste.
Now complexity: O(n * range) with range up to ~110k, n=100 -> 11M, plus copying each time adds same factor. That's fine.
But we must be careful about memory: dp size up to ~110k of ints, fine.
Now we must consider if DP can be optimized or we need to handle large range. But as calculated, worst-case negative sum might be -99900, positive sum 9900, total range 109800, which is okay.
But wait: There is possibility that sum_neg is very large negative and sum_pos is very large positive? Let's compute more carefully: n=100, each a_i up to 100, b_i up to 100, k up to 10. The most negative c_i occurs when a_i is minimal (1) and b_i maximal (100) and k maximal (10): 1 - 1000 = -999. Most positive c_i when a_i maximal (100) and b_i minimal (1) and k minimal (1): 100 - 1 = 99. But if k=10, positive c_i max is 100 - 10*1 = 90. So the range is at most [-99900, 9900] or [-99900, 9000] for k=10. So size ~109k. That's fine.
But note: If k is small like 1, positive can be up to 99, negative can be as low as 1 - 100 = -99? Actually for k=1, c_i = a_i - b_i, with a_i,b_i in [1,100], so c_i range [-99, 99]. So sum_neg max magnitude = 100*99 = 9900. So range up to 19800. So all fine.
Thus DP is acceptable.
However, we must also consider that the DP values (tastes) can be up to sum of all a_i which is at most 100*100=10000. So int is fine.
Now the solution seems straightforward.
But wait: The problem says "the total taste to the total calories ratio of the chosen fruits must equal k." That is sum a_i / sum b_i = k. That implies sum a_i = k * sum b_i. If both sums are zero? But b_i >=1, so sum b_i >0 if at least one fruit. So no division by zero.
Now about the DP approach: Since we are maximizing sum a_i subject to sum c_i = 0, and a_i positive, the DP will always yield the maximum. But we need to ensure that we are not missing any constraints: the subset must be non-empty if no zero items. Our DP includes empty subset, but we check best > 0 to ensure non-empty because a_i >=1.
But consider the case where there are zero items and also non-zero items. Could there be a scenario where the best subset from non-zero items that sums to zero is empty (taste 0) but there is another subset that sums to zero but yields negative taste? Not possible since a_i positive. So empty set is the only one with taste 0. So adding empty set is fine.
Now what about items with c_i = 0 that we added to base_taste? That's correct.
Now we must also think about possibility that there is a subset that sums to zero but includes both zero and non-zero items? We have separated them; zero items always included; non-zero selected subset can be anything.
Now let's test some edge scenarios:
Case: n=1, k=2, a=[5], b=[3] => c=5-6=-1, not zero. So total_taste=0, items: ( -1,5). sum_neg = -1, sum_pos=0, SHIFT=1, size=2? Actually -(-1)=1, size = 0 - (-1) +1 = 2? Wait sum_pos=0, sum_neg=-1 => size = 0 - (-1) +1 = 2. indices 0..1. dp[1]=0. Process item: w=-1, v=5. From s=1 -> ns=0 => new_dp[0]=5. dp[SHIFT]=dp[1]=0. So best=0, total_taste=0 => answer -1. Indeed no subset sums to zero because only one negative weight.
Case: n=2, k=2, a=[5,1], b=[2,2] => c1=5-4=1, c2=1-4=-3? Actually 1-4=-3. So c: 1, -3. Sum to -2. Cannot get zero. So answer -1.
Case: n=2, k=2, a=[4,3], b=[2,1] => c1=0, c2=3-2=1? Actually 3-2=1, so c2=1. So zero items: a1=4, total_taste=4. items: (1,3). sum_neg=0, sum_pos=1, SHIFT=0? Actually sum_neg=0 => SHIFT=0. size=1-0+1=2. dp[0]=0. Process item: w=1, v=3. from s=0 -> ns=1 => new_dp[1]=3. dp[0] remains 0. best = dp[0] = 0. So answer = total_taste + 0 = 4. Is there a subset from non-zero that sums to zero? Only empty. But could we take the non-zero item? It would give sum c=1, not zero, so ratio not k. So answer 4 is correct: choose fruit1 only.
Case: n=2, k=2, a=[6,4], b=[2,3] => c1=6-4=2, c2=4-6=-2. So zero items none. Items (2,6), (-2,4). sum_neg=-2, sum_pos=2, SHIFT=2, size=5? Actually -(-2)=2, size = 2 - (-2) +1 = 5. dp[2]=0. Process (2,6): from s=2->ns=4 => dp[4]=6. Process (-2,4): from s=2->ns=0 => dp[0]=4; from s=4->ns=2 => dp[2]=max(0,6+4=10) = 10. best = dp[2] = 10. So answer = 10. That corresponds to selecting both fruits: sum a=10, sum b=5, ratio=10/5=2, correct.
Now test case where zero items exist but there is also a non-zero subset sum zero that might be better than just zero items? But we always include zero items, so total = base_taste + best_nonzero. That seems correct.
Now potential pitfalls: The DP with copying might be okay, but we could also do in-place to avoid copying. But copying each time might double time but still within limit.
But we need to be careful about initial dp size: we need to compute sum_neg and sum_pos exactly for all items. However note that sum_neg is the sum of negative c_i, which is negative. But is the range of possible totals exactly from sum_neg to sum_pos? Yes, any subset's total c will lie between sum of all negatives (if we include all negative and no positive) and sum of all positives (if we include all positive and no negative). But also we could include some positives and negatives, but the total will be within that interval. So we can set range accordingly.
But what if there are both positive and negative items, but we may also skip some, so the reachable totals are subset sums, but the range is correct for DP indices.
Now we should verify that DP with size sum_pos - sum_neg + 1 indeed covers all possible totals, and index calculation t+SHIFT will be within [0, size-1]. Since SHIFT = -sum_neg, then for t = sum_neg, index = sum_neg + (-sum_neg) = 0; for t = sum_pos, index = sum_pos + (-sum_neg) = sum_pos - sum_neg = size-1? Actually size = sum_pos - sum_neg + 1, so last index = size-1 = sum_pos - sum_neg. So yes.
Now the DP update: we need to ensure we don't go out of bounds when ns = s + w. Since w can be positive or negative, but if we have computed sum_neg and sum_pos based on all items, then any reachable state s corresponds to some total t. Adding w yields new total t' = t + w. Since t is within [sum_neg, sum_pos], and t' = t + w: but is t' guaranteed to be within [sum_neg, sum_pos]? Not necessarily if we only have some items; but since we consider all items, the maximum total when adding w to a state that is already reachable from some subset may exceed the precomputed bounds? For example, suppose we have items with positive w, and we are at state that corresponds to sum of some subset that is less than sum_pos, adding w could exceed sum_pos? Actually if we start from dp[SHIFT] = 0 (total 0) and we only add items, the total after each addition cannot exceed sum of all positive items (if we include all positive) and cannot be less than sum of all negative items. Because the sum of all positive items is sum_pos, and sum of all negative items is sum_neg. So any subset's total is between sum_neg and sum_pos. So when we add an item to a subset total t, the new total t+w is still within those bounds because if w positive, t <= sum_pos - w? Actually consider t could be sum_pos - epsilon, then adding w could exceed sum_pos if w > epsilon. But could t be as high as sum_pos? The maximum total reachable is sum_pos, but if t is sum_pos, adding a positive w would exceed sum_pos. However, can we have a state t = sum_pos? Only if we include all positive items and no negatives. Then adding another positive item would duplicate an item; but each item used at most once, so we wouldn't add a positive item if it's already used. In DP, we process items one by one; for a given item, we only consider adding it to subsets that do not already contain it. So if we are at a state t that was achieved without using this item, then adding the item might produce a total that could be outside the range? However, if we have computed sum_pos as sum of all positive items (including this one), then t + w could be greater than sum_pos if t already includes some other positive items but not this one, and t could be sum_pos - w_other? Actually sum_pos includes all positive items. If we have a subset that doesn't include this item, the maximum total it could have is sum_pos - w (if it includes all other positive items and possibly some negatives). So t <= sum_pos - w? Not necessarily if we include negatives; but the maximum total without this item is sum_pos - w (since all positives except this one). So t can be at most sum_pos - w. Then t + w <= sum_pos. Similarly, for negative items, t could be as low as sum_neg - w (since w is negative, sum_neg includes this negative item). If t is the minimum possible without this item, that is sum_neg - w (since sum_neg includes all negative items). Then t + w >= sum_neg. So indeed, when adding an item not yet used, the new total remains within [sum_neg, sum_pos]. So the bounds hold. So we can safely allocate the full range and ensure ns stays within bounds.
Thus DP with size sum_pos - sum_neg + 1 is safe.
Now we must also consider that if sum_neg is 0 (no negative items) and sum_pos >0, then SHIFT = 0. dp[0] is total c = 0. To achieve total c = 0 with only positive items, we would need to select a subset whose sum of positive numbers is 0, which is only the empty set (since all positive). So DP[0] would remain 0. That's fine.
Similarly if sum_pos=0, only negative items, only empty set yields 0.
Now we should also consider items with c_i = 0. Already handled.
Now complexity: O(n * (sum_pos - sum_neg + 1)). In worst case, sum_pos - sum_neg ≈ 110k, n=100 => 11M operations, each operation includes checking dp[s] and updating. That's fine.
Now implement.
But there is a nuance: The DP using copying each iteration might be slower but still fine. However we can also do in-place: for w > 0, iterate s from max down to min; for w < 0, iterate s from min up to max. But we need to ensure we use previous dp values, not updated within same item. So we can do:
if w > 0:
for s = max_index down to 0:
if dp[s - w] != -INF: dp[s] = max(dp[s], dp[s-w] + v)
But careful: s is index representing total t. The relation: we want to update dp[t] using dp[t - w] from previous state. So if w>0, we iterate s from high to low to avoid using the updated dp[s - w] that already includes this item. That works if we have an array and we update in-place. However, we must be careful about the bounds: s - w must be >=0. Similarly for w<0, we need to iterate s from low to high because w<0 means t - w = t + |w| > t, so we need to use dp[t + |w|] to update dp[t]? Actually the recurrence: include item with weight w (negative) to reach total t: previous total = t - w = t - (negative) = t + |w|, which is larger than t. So we need to iterate s from min to max, checking if dp[s - w] (which is s + |w|) is reachable, and then update dp[s]. Since s - w > s, if we iterate s increasing, then when we update dp[s], dp[s - w] hasn't been updated for this item yet if we haven't processed that index yet? Actually s - w > s, so if we iterate s from low to high, s - w is higher than s, and we haven't updated dp[s - w] in this iteration because we haven't reached that index yet (since we go from low to high). So that's safe. But we must ensure we use the old dp values. So we can implement in-place with two loops based on sign to avoid copying. However, we need to compute min and max indices: we already know size. We'll have dp array of size. We'll initialize with -INF except dp[SHIFT]=0.
Then for each item:
if w > 0:
for s = max_index down to 0:
if s - w >= 0 and dp[s - w] != -INF:
dp[s] = max(dp[s], dp[s - w] + v);
else: // w < 0
for s = 0 to max_index:
if s - w >= 0 && s - w <= max_index && dp[s - w] != -INF:
dp[s] = max(dp[s], dp[s - w] + v);
But careful: In the case w negative, s - w = s + |w|, which is >= s. So condition s - w <= max_index ensures it's within bounds.
That works without copying. But we must be careful that dp[s - w] refers to the state before processing this item; as we iterate s increasing, dp[s - w] is a state with larger index, which we haven't updated yet in this iteration because we go from low to high; thus it's still the old value. That's correct. So we can do in-place.
We'll compute max_index = size - 1.
Now note: w can be negative; we need to loop s from 0 to max_index (inclusive). But careful: when w is negative, s - w might be out of bounds. We'll check bounds.
But let's test with earlier example: items: (6,10) positive, then (-6,8) negative, then (-1,1) negative.
Initialize dp[7]=0.
Processing (6,10): w>0, iterate s from max down to 0. max_index? size=14? Actually sum_neg=-7, sum_pos=6 => SHIFT=7, size=14, max_index=13. Loop s from 13 down to 0:
s=13: s-w=19 out of bounds? 13 - (-6) = 19 >13, skip.
Works. So dp[7] becomes 18.
Processing (-1,1): w=-1. Loop s=0..13:
s=0: s-w=1, dp[1]=8 => dp[0]=9.
s=1: s-w=2, dp[2] -INF skip.
s=6: s-w=7, dp[7]=18 => dp[6]=19.
s=7: s-w=8, dp[8] -INF skip.
s=12: s-w=13, dp[13]=10 => dp[12]=11.
s=13: s-w=14 out of bounds.
Works. Final dp[7]=18.
So in-place works.
Now we also need to consider that we might have many items and the DP array might become large. In-place updates require careful ordering but is fine.
Thus we can implement DP in-place to avoid copying.
Now we need to define INF as some large negative number, maybe -1e9, because max sum a_i is up to 10000.
Implementation details:
Read input.
Compute c_i = a_i - k*b_i.
Initialize base = 0.
Vectors pos_items, neg_items? Actually we can keep a single list for non-zero items.
Compute sum_neg and sum_pos as described.
SHIFT = -sum_neg.
size = sum_pos - sum_neg + 1.
dp.assign(size, -INF). Use e.g., -1e9.
dp[SHIFT] = 0.
For each item (w, v):
if w > 0:
for s = size-1 down to 0:
if s - w >= 0 && dp[s - w] != -INF:
dp[s] = max(dp[s], dp[s - w] + v);
else: // w < 0
for s = 0 to size-1:
if s - w >= 0 && s - w < size && dp[s - w] != -INF:
dp[s] = max(dp[s], dp[s - w] + v);
After processing, best = dp[SHIFT].
But careful: In the w<0 loop, s - w might be larger than size-1, so we need to check < size. Also s - w might be negative? Since w negative, s - w = s + |w| >= s >=0, so non-negative, only check < size.
Now one more nuance: In the w>0 loop, we iterate from high to low; s - w can be any index. We need to ensure we don't access negative.
Now after DP, best is max additional taste.
Now answer determination.
Potential issue: There might be multiple subsets giving sum 0, but DP picks max taste. Good.
Now check for the case where base_taste=0 and best=0: answer -1. However, consider if there is a subset that yields sum 0 but with taste 0? impossible.
Now also consider if base_taste>0 and best=0, answer = base_taste.
Now consider scenario where there are both zero and non-zero items, and there is a subset of non-zero that sums to zero but with negative taste? Not possible because a_i positive.
Now check if there is possibility that the best subset includes some zero items? No, we already separated. But what about zero items that we have already added to base_taste? That's fine.
Now confirm with sample.
Now consider edge-case where sum_neg and sum_pos are huge but still manageable. As argued, max size about 110k, dp of ints ~440KB, fine.
Now consider possibility that sum_neg and sum_pos are both zero, i.e., all items have c_i=0? Actually then items list is empty, we already handled. So DP not needed.
Now consider case where there is one item with c_i=0, but we also have other items. Since c_i=0, we added it to base, then for remaining items, DP might produce sum zero. That's okay.
Now about the requirement "at least one fruit". If base_taste>0, we already have at least one fruit (the zero items). So empty additional set is allowed. If base_taste=0, we need best>0.
Thus final answer.
Now we should also think about alternative solutions: maybe using DP with map or something, but this is fine.
Case: n=2, k=3, a=[6,8], b=[2,3] => c1=0, c2=8-9=-1. base=6. items: (-1,8). sum_neg=-1, sum_pos=0, SHIFT=1, size=2. dp[1]=0. Process (-1,8): w negative: loop s=0..1:
s=0: s-w=1, dp[1]=0 -> dp[0]=8.
s=1: s-w=2 out of bounds. dp[1] remains 0.
best=dp[1]=0. So answer=6+0=6. That corresponds to only taking first fruit. Could we take both? sum a=14, sum b=5, ratio=14/5=2.8 not equal to 3. So not valid. So answer 6 correct.
Case: n=3, k=2, a=[10,8,2], b=[2,7,1] -> c1=6, c2=-6, c3=0? 2-2=0. base=2. items: (6,10), (-6,8). sum_neg=-6, sum_pos=6, SHIFT=6, size=13. dp[6]=0. Process (6,10): positive: update dp[12]=10. Process (-6,8): negative: update dp[0] from dp[6]? Actually s from 0..12: s=0? s=0: s-w=6, dp[6]=0 -> dp[0]=8; s=6: s-w=12, dp[12]=10 -> dp[6]=max(0,18)=18; s=12: s-w=18 out. So best=dp[6]=18. answer= base+18=20. That would be total taste=20 (10+8+2). Check ratio: sum a=20, sum b: 2+7+1=10, ratio=2. Yes. So good.
Now, consider possibility that there are multiple zero items and also non-zero items that sum to zero but some zero items are not included? As argued, we include all zero items.
Now, but we should also consider if including all zero items might make the ratio no longer exactly k? Actually each zero item individually satisfies a_i = k*b_i, so adding them to any set that already has sum a = k sum b will preserve the ratio. So it's safe.
Thus solution is correct.
Now implementation details: We'll use int for dp with -1e9 as -INF. Actually maximum taste sum is 100*100=10000, so -1e9 is safe.
Compute sum_neg and sum_pos: we need to loop through items (non-zero). But note: we might have both positive and negative. However, careful: sum_neg is sum of negative c_i (which are negative numbers). So we can compute:
int sum_neg = 0, sum_pos = 0;
for each item:
if (w > 0) sum_pos += w;
else sum_neg += w; // w is negative
Now dp array: vector dp(size, -INF). We'll set INF as -1e9.
Then dp[shift] = 0.
Processing items:
for (auto [w, v] : items) {
if (w > 0) {
for (int s = size-1; s >= w; --s) { // because s-w >=0
if (dp[s - w] != -INF) {
dp[s] = max(dp[s], dp[s - w] + v);
}
}
} else {
// w < 0, let wneg = -w (positive)
// s from 0 to size-1-(-w) maybe
// Since s - w = s + |w| <= size-1 => s <= size-1 - |w|
// But we can just loop s from 0 to size-1 and check condition.
for (int s = 0; s < size; ++s) {
int ns = s - w; // because w negative => s - w = s + |w|
if (ns < size && dp[ns] != -INF) {
// Actually we need to use old dp[ns] to update dp[s]
// But careful: we are reading dp[ns] which might have been updated earlier in this loop for negative w. Since we are iterating s increasing, ns > s, so dp[ns] hasn't been updated yet because ns > s and we haven't reached it. That's correct.
// However, we must ensure we don't inadvertently use dp[ns] that was updated from a previous item (which is fine) but we need to ensure we don't use the same item multiple times. Since we iterate s increasing, and ns > s, when we later process ns as s, we will read dp[??] but that's for a different state. The key is that we are reading dp[ns] which could have been updated earlier in this same loop if we already passed that index? Since ns > s, and we are going from low to high, we haven't processed ns yet, so dp[ns] still contains value from before this item. Good.
if (dp[s] < dp[ns] + v) {
dp[s] = dp[ns] + v;
}
}
}
}
}
But note: In the w<0 loop, we are using dp[ns] to update dp[s]. But dp[ns] could be -INF initially. That's fine.
One subtlety: In the w>0 loop, we are iterating s from high to low. For each s, we check dp[s - w] and update dp[s]. This uses dp[s - w] which hasn't been updated in this iteration because we are going downwards, so s - w < s, and we haven't updated dp[s - w] yet in this iteration (since we started from high). So correct.
Now after processing all items, best = dp[shift].
Now answer.
Now we must consider that the DP array size may be large (up to 110k) and we have up to 100 items, each with loops over size, that's about 11 million operations, which is fine.
Now test edge: sum_neg and sum_pos might be zero (if there are no non-zero items). Then size = 1, shift = 0. dp[0]=0. No items, best=0. Then base_taste may be >0. answer = base_taste.
Now what if there are items but both sum_neg and sum_pos are zero? That would mean all c_i = 0? But we excluded zero items, so all non-zero items have non-zero c, so at least one of sum_neg or sum_pos is non-zero. So size > 1.
Now we should also think about potential overflow of shift or size if sum_neg is very negative and sum_pos is large? But we computed ranges; they fit in int (max size ~110k). Use int.
Now let's double-check the bound: c_i = a_i - k * b_i. Maximum positive: a_i up to 100, b_i min 1, k min 1 => max positive = 100 - 1 = 99. So sum_pos <= 99100 = 9900. Minimum negative: a_i min 1, b_i max 100, k max 10 => c_i = 1 - 1000 = -999, sum_neg >= -999100 = -99900. So size = sum_pos - sum_neg + 1 <= 9900 - (-99900) + 1 = 109801. So about 110k. That's fine.
Now one more nuance: In DP, when we compute sum_neg and sum_pos, we consider all items. However, if there are many items, the shift could be up to 99900, which is within int. But we need to allocate vector of size 110k, that's okay.
Now we must ensure we don't have memory issues with multiple copies. We are using in-place, no copies.
Now check if there is any case where best might be negative? No, because a_i positive, so any subset gives positive taste unless empty (0). So dp[shift] >= 0 always. But we initialize dp with -INF and set dp[shift]=0; after updates, it remains non-negative if any reachable non-empty subset yields positive taste. But could there be a subset that yields zero taste? Only empty set. So dp[shift] will be either 0 (empty) or positive.
Now about the condition of at least one fruit: If base_taste==0 and best>0, we have non-empty subset from non-zero items. If base_taste==0 and best==0, then no non-empty subset yields sum c=0, so answer -1.
But consider scenario where base_taste>0 and best=0, we output base_taste, which includes at least one fruit. That's fine.
Now we should also consider that there might be items with c_i=0 that we included in base_taste. But what if there are no non-zero items? Then answer = base_taste. That's correct.
Now any other tricky aspects? Possibly the DP might consider subsets that include zero items? No, we removed them.
Now confirm: Is it always optimal to include all zero items? Suppose we have zero items and some non-zero items. Including zero items adds taste without affecting ratio. So any optimal solution can be improved by adding any zero item not already included. So yes.
Thus solution is correct.
Now we need to code. We'll use fast I/O maybe not needed.
Edge Cases: When SHIFT is large, but that's okay.
Let's think about potential bug: In DP for negative w, we used if (ns < size && dp[ns] != -INF). But we also need to consider that dp[ns] might have been updated earlier in this same loop if ns < s? However ns = s - w > s because w < 0 => -w >0, so ns > s. Since we iterate s from low to high, ns > s means we haven't processed ns yet, so dp[ns] is from previous item iterations (or initial). So it's safe.
But what about when we have multiple negative items? The order of processing doesn't matter as we are doing 0/1 knapsack correctly because we iterate s increasing for negative weights. That's correct.
Now we should test with a case where there are both positive and negative and many items.
Now I'll also consider alternative approach: Since k <= 10 and a_i,b_i up to 100, we could also use DP on total calories or taste, but the given approach is fine.
Now I'll produce final solution.
But before finalizing, check the note: In second sample, all c_i = -2, no zero items, sum c_i can't be zero except empty. So answer -1. Works.
Now test with a case where there are zero items and non-zero items that sum to zero but with less total taste than some other combination? DP will find max taste.
Now consider potential integer overflow? Not.
Now we should ensure that we treat SHIFT as int and size as int.
Implementation steps in code:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9; // use negative INF for unreachable
int main() {
int n, k;
cin >> n >> k;
vector a(n), b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
code
int base_taste = 0;
vector<pair<int,int>> items; // (c, a)
for (int i = 0; i < n; i++) {
int c = a[i] - k * b[i];
if (c == 0) {
base_taste += a[i];
} else {
items.push_back({c, a[i]});
}
}
// if no non-zero items, answer is base_taste (which must be >0 because n>=1 and a_i>=1)
if (items.empty()) {
cout << base_taste << endl;
return 0;
}
int sum_neg = 0, sum_pos = 0;
for (auto &[c, taste] : items) {
if (c > 0) sum_pos += c;
else sum_neg += c; // c negative
}
int SHIFT = -sum_neg;
int size = sum_pos - sum_neg + 1; // since sum_neg negative, -sum_neg positive
const int NEG_INF = -1e9;
vector<int> dp(size, NEG_INF);
dp[SHIFT] = 0;
for (auto &[w, v] : items) {
if (w > 0) {
for (int s = size-1; s >= w; --s) {
if (dp[s - w] != NEG_INF) {
dp[s] = max(dp[s], dp[s - w] + v);
}
}
} else { // w < 0
// iterate s from 0 to size-1
// but we can restrict s <= size-1 - (-w) to avoid out-of-bounds check
int w_abs = -w;
for (int s = 0; s < size - w_abs; ++s) { // because ns = s + w_abs < size => s < size - w_abs
int ns = s + w_abs; // s - w
if (dp[ns] != NEG_INF) {
dp[s] = max(dp[s], dp[ns] + v);
}
}
}
}
int best = dp[SHIFT];
int ans;
if (base_taste > 0) {
ans = base_taste + best;
// best could be 0; still valid
cout << ans << endl;
} else {
if (best > 0) {
ans = best;
cout << ans << endl;
} else {
cout << -1 << endl;
}
}
return 0;
Test 2: n=1, k=5, a=[6], b=[1] => c=6-5=1 => base_taste=0, items (1,6). sum_neg=0, sum_pos=1, SHIFT=0, size=2. dp[0]=0. Process w=1>0: loop s from 1 down to 1? Actually size-1=1, w=1, loop s=1: if dp[0]!=NEG_INF => dp[1]=6. dp[0] remains 0. best=dp[0]=0. base_taste=0, best=0 => output -1. Indeed sum a=6, sum b=1, ratio=6, not 5. So -1.
Test 4: n=3, k=3, a=[6,3,9], b=[2,1,3] => c1=0, c2=0, c3=9-9=0 => all zero, base_taste=18.
Test 5: n=3, k=3, a=[6,4,8], b=[2,1,2] => c1=0, c2=4-3=1, c3=8-6=2. base_taste=6. items: (1,4),(2,8). sum_neg=0, sum_pos=3, SHIFT=0, size=4. dp[0]=0.
Process (1,4): w>0: update dp[1]=4.
Process (2,8): w>0: from s=3 down to 2: s=3: if dp[1]!=-inf => dp[3]=12; s=2: if dp[0]!=-inf => dp[2]=8. best=dp[0]=0 => answer=6. Could we include both non-zero? total c=3, not zero. Could we include only one? c=1 or 2, not zero. So only base works. Ratio with base only: sum a=6, sum b=2, ratio=3. Good.
Test 6: n=3, k=3, a=[6,4,5], b=[2,1,2] => c1=0, c2=1, c3=5-6=-1. base=6. items: (1,4),(-1,5). sum_neg=-1, sum_pos=1, SHIFT=1, size=3. dp[1]=0.
Process (1,4): w>0: loop s from 2 to 1? size-1=2, w=1: s=2: if dp[1]!=-inf => dp[2]=4; s=1: if dp[0]? dp[0] -inf skip.
Process (-1,5): w<0, w_abs=1: loop s=0 to size-1-1=1? Actually size - w_abs = 3-1=2, so s from 0 to 1 inclusive? Actually s < size - w_abs, so s max = size - w_abs -1? Let's do: we want ns = s + w_abs < size => s < size - w_abs. So s max = size - w_abs - 1 if integer. For size=3, w_abs=1, s < 2 => s=0,1. So:
s=0: ns=1, dp[1]=0 => dp[0]=5.
s=1: ns=2, dp[2]=4 => dp[1]=max(0,4+5=9)=9.
After that, best = dp[SHIFT]=dp[1]=9. So answer = base+9 = 15. That corresponds to selecting all three fruits: total a=6+4+5=15, total b=2+1+2=5, ratio=3. Good.
Now test: base_taste=0 but there is a zero-item? Actually base_taste=0 only if no zero items.
Now what about a scenario where there are zero items and the best from non-zero is empty, answer = base_taste. That's fine.
Now we should also consider case where dp[SHIFT] could be negative? Not possible.
Now final nuance: The DP might produce dp[SHIFT] from an empty subset (0). In case base_taste=0, we need non-empty, so we check best > 0. However, consider if there is a subset that yields total taste 0? That would require all a_i in subset to be zero, but a_i >= 1, so impossible. So best > 0 iff non-empty subset exists.
Now, but what about if base_taste = 0 and there is a subset with sum c=0 that has taste >0? Then best>0.
Now we should double-check the indexing for negative w loop: the condition s < size - w_abs ensures ns = s + w_abs < size. That's safe. But what about when s - w is also >=0? It always is, because w negative, s - w = s + |w| >=0. So only need to check upper bound.
Now there is a subtle bug: In negative w loop, we used dp[ns] to update dp[s]. But we must ensure we are using the dp values from before the current item, not after updates that might have happened for lower s. Since we iterate s increasing, and ns > s, dp[ns] hasn't been updated in this iteration. So that's correct.
But what about if we restrict loop to s < size - w_abs? That's correct. However we must also consider the case where w_abs is zero? But w != 0 because we excluded zero items. So fine.
Now in the positive w loop, we iterated s from size-1 down to w. That ensures s - w >= 0. Good.
Now we need to confirm that the dp array is properly sized to allow indices up to sum_pos - sum_neg. For positive w, s goes up to size-1, which corresponds to total c = sum_pos. For negative w, s goes from 0 to size-1 - w_abs, which corresponds to totals from sum_neg? Actually s represents total c + SHIFT. The smallest s is 0 (total c = sum_neg). For negative w, we need to ensure we don't access dp[ns] where ns >= size. The condition s < size - w_abs ensures ns < size. Also, we might have dp indices that are out of range for s near the top, but we skip those.
Now about the case where SHIFT may be large; but dp array size is size, which could be up to 110k, okay.
Now memory: dp vector of ints size ~110k, okay.
Now one more thought: Could there be an issue with the DP due to using -INF as a very negative number, and then adding v might overflow? v <= 100, n <= 100, max sum a_i <= 10000, so dp values are within 10000, adding v is fine. -1e9 is safe.
Now implement.
But wait: There's another approach: Instead of DP on c_i, we can treat each fruit as having "value" a_i and "cost" c_i, and we want to maximize value with cost exactly 0. This is exactly a knapsack with possibly negative costs.
Now I want to check a potential subtlety: The condition is sum a_i / sum b_i = k exactly, not approximate. Because a_i, b_i are integers, k is integer up to 10. So sum a_i = k * sum b_i, i.e., sum (a_i - k b_i) = 0. That's correct.
Now verify with sample 1: we got 18.
Now sample 2: we got -1.
All good.
Now what about large n and large values? Complexity ok.
Now test edge: n=100, all c_i = 0. Then base_taste = sum a_i, answer is sum a_i. Our code: items empty, we output base_taste directly. Good.
Now test: n=100, all c_i positive (e.g., a_i=100, b_i=1, k=1 => c_i=99). Then sum_neg=0, sum_pos=9900, SHIFT=0, size=9901. DP will process all positive weights. dp[0]=0. After processing all items, dp[0] remains 0 because cannot get sum 0 with positive weights except empty. base_taste=0 (if no zero items). So best=0 => answer -1. Indeed, with all positive c_i, any non-empty subset sum >0, cannot be 0.
Now test all c_i negative similar.
Now test mixed with large numbers.
Now consider scenario where SHIFT is large positive but we have few items, dp array size large but many states unreachable. That's okay.
Now check potential off-by-one in size calculation: size = sum_pos - sum_neg + 1. sum_neg negative, so sum_pos - sum_neg = sum_pos + |sum_neg|. For example, sum_neg = -7, sum_pos = 6 => size = 6 - (-7) + 1 = 14. That's correct as earlier. Index range from 0 to 13 inclusive. SHIFT = -sum_neg = 7, so index 7 corresponds to total=0. That matches.
Now when we compute dp[SHIFT] we set to 0.
Now about items with negative weight: we need to ensure that when we update, we don't go out of bounds. With w negative, we use ns = s - w = s + |w|. Since s max is size-1 - |w|, ns max = size-1. So safe.
Now we should also consider if sum_pos - sum_neg could be zero? That would happen if sum_pos = sum_neg = 0. But that would imply all c_i = 0, but we have excluded zeros, so if there is any non-zero c_i, either sum_pos > 0 or sum_neg < 0. So size >= 2.
Now check for integer overflow in sum_neg and sum_pos: c_i can be as low as -999, n=100, sum_neg minimum -99900, sum_pos max 9900, so they fit in int.
Now one more nuance: The DP approach might be done with map/unordered_map to reduce memory if range is large, but range is acceptable.
Now answer printing: If base_taste > 0, we output base_taste + best (which could be 0). However, is it possible that best is negative? No.
But consider if base_taste > 0 and best is negative? Not possible. So answer is int.
Now test with custom case: n=3, k=1, a=[2,3,5], b=[2,3,5] => all c_i=0. base_taste=10. answer=10.
Now test with n=3, k=1, a=[2,3,5], b=[2,3,4] => c: 0,0,1? Actually 5-4=1. base_taste=5. items: (1,5). sum_neg=0, sum_pos=1, SHIFT=0, size=2. dp[0]=0. process (1,5): dp[1]=5. best=dp[0]=0. answer=5. But could we include all three? sum a=10, sum b=9, ratio=10/9 not 1. Only first two give ratio=5/5=1. So answer 5 correct.
Now test with base_taste=0 but there is a subset that sums to 0 with positive taste, but also empty set gives 0. best>0, answer best.
Now verify with case where best=0 but there is a subset that sums to 0 with taste 0? impossible.
Now think about DP initial state: dp[SHIFT] = 0. But could there be a subset that sums to zero but with total taste 0? Only empty set. So we are fine.
Now confirm that we are maximizing taste, not minimizing. DP uses max.
Now the solution should pass.
But wait: Are there any constraints about k up to 10, but a_i, b_i up to 100. So c_i range as described.
Now we should also consider alternative possibility: The DP might also consider subsets that achieve sum zero but with some items having c_i=0? We removed those items. So that's okay.
Now code.
Potential pitfalls: When we compute sum_neg and sum_pos, we need to use long long? Actually int is fine as max absolute about 100k.
Now also we must be careful about using int for dp indices. Should be fine.
Now finalize solution.
But let's double-check with a case where there are many items and the DP might produce best > 0 but we need to consider that we already included zero items; that is fine.
Now we could also include zero items after DP? Already included.
Now we must ensure that we treat base_taste correctly when there are zero items and also non-zero items that we don't select. Our DP finds best additional taste from non-zero items that sum to zero. But could there be a scenario where not including all zero items yields a higher total taste? Suppose we have zero items with taste, and non-zero items that sum to zero but include some zero items? Wait, zero items have c_i=0, so they don't affect sum condition. So if