#C975. Weight Manager
Weight Manager
Weight Manager
This problem involves implementing a Weight Manager data structure that manages a list of item weights along with a fixed capacity. The data structure supports two operations:
- updateWeight: Update the weight at a given index.
- canCarry: Determine whether it is possible to choose a subset of items (by either taking or skipping each item in order from a given index) such that the total weight of the chosen items does not exceed a specified remaining capacity. Note that the empty subset is allowed.
The recursive condition is formally defined as follows. Let \( f(i, r) \) be true if from index \( i \) onward, one can pick a subset so that after subtracting the weights of picked items the remaining capacity \( r \) stays nonnegative. Then, we have: \[ f(i, r)=\begin{cases} \text{False} & \text{if } r<0,\\ \text{True} & \text{if } i \geq n,\\ f(i, r)=f(i+1, r)\lor f(i+1, r-w_i) & \text{otherwise.} \end{cases} \]
Your task is to implement the Weight Manager with the two operations described above and process a series of queries.
inputFormat
The first line contains two integers ( n ) and ( capacity ), where ( n ) is the number of items. The second line contains ( n ) space-separated integers representing the weights. The third line contains an integer ( Q ), the number of queries. Each of the following ( Q ) lines contains a query in one of the following formats:
- For an update operation:
1 index newWeight
- For a query operation:
2 index remainingWeight
Note: The queries of type 2 require you to output the boolean result (True or False) on a new line.
outputFormat
For each query of type 2, output "True" or "False" on a single line, indicating whether it is possible to carry a selection of items (starting from the provided index) such that the total weight does not exceed the given remaining weight.## sample
5 5
2 1 3 4 5
3
2 0 5
1 2 2
2 0 5
True
True
</p>