#K48477. Taco's Taste Balance
Taco's Taste Balance
Taco's Taste Balance
You are given two sets of ingredient packets. Each packet contains a certain weight of an ingredient. For each test case, your task is to determine whether it is possible to exactly measure out the required amounts for both ingredients using any combination of the available packets.
This is a variation of the subset sum problem. In other words, given a list of packet weights and a target weight, determine whether any subset of these packets sums exactly to the target. You need to perform this check for both ingredients independently.
Input Format:
The first line contains the number of test cases. For each test case, the input is structured as follows:
- An integer n, the number of packets for ingredient A.
- A line with n space-separated integers representing the weights of ingredient A packets.
- An integer m, the number of packets for ingredient B.
- A line with m space-separated integers representing the weights of ingredient B packets.
- A line with two integers x and y, the target weights required for ingredient A and B respectively. In mathematical notation, you are to check if
\(\exists\, S_A \subseteq \text{weightsA}\) such that \(\sum_{w \in S_A} w = x\) and \(\exists\, S_B \subseteq \text{weightsB}\) such that \(\sum_{w \in S_B} w = y\).
Output Format:
For each test case, output a single line with YES
if both target weights can be measured exactly. Otherwise, print NO
.
inputFormat
The input is given from standard input and follows the format below:
T n w1 w2 ... wn m w1 w2 ... wm x y ... (repeated for T test cases)
where T is the number of test cases.
outputFormat
For each test case, output one line containing either YES
or NO
depending on whether both ingredients can be measured out exactly using a subset of the available packets.
1
3
1 3 5
2
2 4
6 6
YES