To solve this problem, we need to determine the maximum number of reward packages (K) that the school can prepare without exceeding the available budget (S). Each reward package requires a certain number of books (T0), pens (V0), and bags (C0). The school already has some of these items and can purchase additional ones if needed, subject to the budget constraint.
Problem Breakdown
-
Inputs:
S: Total available money.
T0, V0, C0: Number of books, pens, and bags required per reward package.
T, V, C: Already available books, pens, and bags.
ct, cv, cc: Cost of one book, pen, and bag respectively.
-
Objective:
- Maximize
K such that the total cost of purchasing additional items does not exceed S.
-
Constraints:
1 ≤ S ≤ 10^12
0 ≤ T0, V0, C0 ≤ 10
1 ≤ T, V, C ≤ 100
1 ≤ ct, cv, cc ≤ 100
Approach
Given the potential size of S (up to (10^{12})), a direct brute-force approach would be inefficient. Instead, we'll use a binary search method to efficiently find the maximum K.
-
Binary Search Setup:
- Low (
lo): 0 (minimum possible packages).
- High (
hi): A reasonable upper bound, such as S + max(T0, V0, C0) to ensure coverage.
-
Binary Search Process:
- Calculate the mid-point
mid = (lo + hi) // 2.
- Determine the additional number of books, pens, and bags needed:
need_T = max(mid * T0 - T, 0)
need_V = max(mid * V0 - V, 0)
need_C = max(mid * C0 - C, 0)
- Calculate the total cost:
total_cost = need_T * ct + need_V * cv + need_C * cc.
- If
total_cost is within the budget (≤ S), update the answer and search in the higher half.
- Otherwise, search in the lower half.
-
Termination:
- The binary search continues until
lo exceeds hi. The last valid mid that fits within the budget will be the maximum K.
Example Walkthrough
Let's walk through the first example provided:
Input:
5
3 2 1
5 4 1
1 2 3
- Budget (
S): 5
- Per Package Requirements: 3 books, 2 pens, 1 bag
- Available Items: 5 books, 4 pens, 1 bag
- Costs: 1 per book, 2 per pen, 3 per bag
Calculation:
-
For K=2:
- Additional books needed:
max(2*3 - 5, 0) = 1
- Additional pens needed:
max(2*2 - 4, 0) = 0
- Additional bags needed:
max(2*1 - 1, 0) = 1
- Total cost:
1*1 + 0*2 + 1*3 = 4 ≤ 5 → Valid
-
For K=3:
- Additional books needed:
max(3*3 - 5, 0) = 4
- Additional pens needed:
max(3*2 - 4, 0) = 2
- Additional bags needed:
max(3*1 - 1, 0) = 2
- Total cost:
4*1 + 2*2 + 2*3 = 14 > 5 → Invalid
Thus, the maximum K is 2.
Implementation
Below is a Python implementation based on the above approach:
def max_rewards(S, T0, V0, C0, T, V, C, ct, cv, cc):
def is_possible(K):
need_T = max(K * T0 - T, 0)
need_V = max(K * V0 - V, 0)
need_C = max(K * C0 - C, 0)
total_cost = need_T * ct + need_V * cv + need_C * cc
return total_cost <= S
lo = 0
hi = S # Upper bound can be set as S since each item costs at least 1
ans = 0
while lo <= hi:
mid = (lo + hi) // 2
if is_possible(mid):
ans = mid
lo = mid + 1
else:
hi = mid - 1
return ans
# Example Usage:
# Sample Input 1
S = 5
T0, V0, C0 = 3, 2, 1
T, V, C = 5, 4, 1
ct, cv, cc = 1, 2, 3
print(max_rewards(S, T0, V0, C0, T, V, C, ct, cv, cc)) # Output: 2
# Sample Input 2
S = 5
T0, V0, C0 = 3, 2, 2
T, V, C = 2, 4, 1
ct, cv, cc = 3, 2, 3
print(max_rewards(S, T0, V0, C0, T, V, C, ct, cv, cc)) # Output: 0
Explanation of the Code:
-
Function max_rewards:
- Parameters:
S: Total budget.
T0, V0, C0: Items required per package.
T, V, C: Available items.
ct, cv, cc: Costs of each item.
is_possible(K): Helper function to check if it's possible to prepare K packages within the budget.
- Binary Search: Iteratively narrows down the range to find the maximum valid
K.
-
Example Usage:
- Demonstrates how the function works with the provided sample inputs.
Conclusion
By using a binary search approach, we efficiently determine the maximum number of reward packages that can be prepared within the given budget, considering both the already available items and the cost of purchasing additional ones.