#K4606. Maximum Storage Optimization
Maximum Storage Optimization
Maximum Storage Optimization
You are given a warehouse with a storage capacity \(C\) and \(N\) shipments, each with a given volume. Your task is to select a subset of these shipments such that the total volume is maximized without exceeding the capacity \(C\). This is equivalent to a simplified 0/1 knapsack problem where each item’s weight is equal to its value.
Input Constraints:
- \(1 \leq N \leq 10^5\)
- \(0 \leq \text{volume}_i \leq C\)
- \(0 \leq C \leq 10^5\)
The optimal solution can be found using a dynamic programming approach. In this problem, the state \(dp[j]\) represents the maximum volume that can be achieved with a capacity of \(j\). The recurrence relation is:
[ dp[j] = \max(dp[j], dp[j - v] + v) \quad \text{for each shipment with volume } v \text{ and for } j \geq v. ]
Your program should read from standard input and write the result to standard output.
inputFormat
The first line contains two integers: \(N\) (the number of shipments) and \(C\) (the warehouse capacity). The second line contains \(N\) space-separated integers representing the volumes of the shipments.
Example:
4 10 5 4 7 3
outputFormat
Output a single integer representing the maximum total volume that can be selected without exceeding the capacity \(C\).
Example:
10## sample
4 10
5 4 7 3
10