
1) He must select exactly one wrapper to wrap all the chosen gifts.
2) He can select one or more gifts or no gifts at all.
3) He has 2 copies of the same gift. So, he can take 0 or 1 or 2 copies of each gift.
Let’s say you have two wrappers with cost [1, 2] and three gifts with cost [3, 4, 5] and ‘X’ = 7. In the first case, you select wrapper number ‘2’ and gift number ‘1’ and ‘3’, so your total cost will be 2 + 3 + 5 = 10. While in the second case, you select wrapper number ‘1’ and gift number ‘2’, so your total cost will be 1 + 4 = 5. So out of both the cases, second case is closer to ‘X’ as abs(7 - 5) = 2 and abs(7 - 10) = 3.
First-line contains ‘T’, denoting the number of Test cases.
For each Test case:
The first line contains three space-separated integers, ‘N’, ‘M’ and ‘X’, denoting the number of the Wrappers, Gifts and closest number till you have to get the total cost, respectively.
The following line contains an array ‘W’ of ‘N’ space-separated integers, denoting the cost of the ‘ith’ wrapper.
The following line contains an array ‘G’ of ‘M’ space-separated integers, denoting the cost of the ‘ith’ gift.
For each test case, print an integer denoting the total cost that is as close as ‘X’.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10
1 <= ‘M’ <= 10
1 <= W[i], G[i] <= 10^4
1 <= ‘X’ <= 10^9
Time Limit: 1 sec
Algorithm:
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators
Gray Code Transformation
Count of Subsequences with Given Sum