#K46477. Box Packing Problem
Box Packing Problem
Box Packing Problem
You are given a set of products and a set of boxes. Each product has a weight and each box has a weight limit. Your task is to determine if it is possible to pack all the products into the boxes such that no box exceeds its weight limit.
For a valid packing, the total weight of products must satisfy the inequality:
[ \sum_{i=1}^{N} w_i \le \sum_{j=1}^{M} L_j ]
where \(w_i\) represents the weight of the \(i\)-th product and \(L_j\) represents the weight limit of the \(j\)-th box. In addition, you can pack at most one product into one box in each step according to a greedy strategy. Decide whether a valid packing exists.
If it is possible, output Possible
, otherwise output Impossible
.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains a single integer
T
which denotes the number of test cases. - For each test case, the input is given in four lines:
- The first line contains an integer
N
denoting the number of products. - The second line contains
N
space-separated integers representing the weights of the products. - The third line contains an integer
M
denoting the number of boxes. - The fourth line contains
M
space-separated integers representing the weight limits of the boxes.
- The first line contains an integer
outputFormat
For each test case, output a single line to stdout
containing either Possible
if the products can be successfully packed into the boxes under the given constraints or Impossible
otherwise.
3
5
10 20 10 30 40
3
50 50 30
4
15 35 25 10
3
20 30 40
3
5 10 20
2
15 25
Possible
Impossible
Possible
</p>