#K41702. Optimal Packet Stacking
Optimal Packet Stacking
Optimal Packet Stacking
You are given a shelf of width \(W\) and \(N\) packets. The \(i\)-th packet has a width \(w_i\) and height \(h_i\). Packets are arranged in a single row. The goal is to select and arrange some packets (in an order sorted by descending width) such that the cumulative width does not exceed \(W\) and the overall height of the stack (which is the maximum height among the selected packets) is minimized.
Formally, if you select packets with indices \(i_1, i_2, \dots, i_k\) (sorted so that \(w_{i_1} \ge w_{i_2} \ge \dots \ge w_{i_k}\)) satisfying \(\sum_{j=1}^{k} w_{i_j} \le W\), the resulting stack height is \(\max_{1 \le j \le k} h_{i_j}\). Compute the minimum possible tallest stack height.
inputFormat
The first line contains an integer \(T\) denoting the number of test cases. Each test case consists of three lines:
- The first line contains two integers \(N\) and \(W\) — the number of packets and the width of the shelf respectively.
- The second line contains \(N\) space-separated integers \(w_1, w_2, \dots, w_N\) representing the widths of the packets.
- The third line contains \(N\) space-separated integers \(h_1, h_2, \dots, h_N\) representing the heights of the packets.
outputFormat
For each test case, output a single integer – the minimum possible height of the tallest stack that can be obtained.
## sample1
3 10
2 3 5
1 4 9
9
</p>