| 2026 Spring UT CS104c Midterm #2 |
|---|
| Finished |
You've collected a large number of gift certificates to a certain online store. You now want to redeem them for as much swag as possible. Each certificate has a dollar value, as does every item in the online store. One gift certificate can buy only one item of equal or lesser value from the store: you cannot group purchases together to buy multiple items with one certificate, or one item using multiple certificates of lesser value. Two different certificates can be used to buy (two copies of) the same item. A gift certificate with value less than the cheapest item in the store cannot buy anything.
Given the values of your gift certificates, what is the maximum total value of items you can buy with them from the online store?
The first line of input contains two space-separated integers $$$N$$$ and $$$M$$$, the number of items in the store $$$(1 \leq N \leq 200\,000)$$$ and the number of gift certificates that you own $$$(1 \leq M \leq 200\,000)$$$.
The next line contains $$$N$$$ space-separated integers $$$s_i$$$ $$$(1 \leq s_i \leq 10^9)$$$, the value of the items (in dollars) available for purchase from the store. The item values are listed in non-decreasing order.
The final line contains $$$M$$$ space-separated integers $$$c_i$$$ $$$(1 \leq c_i \leq 10^9)$$$, the value of the gift certificates you own (in dollars).
Print the maximum total value (in dollars) of items you can buy using your gift certificates.
3 420 50 1015 50 51 100
150
1 3100010 500 500
0
5 5240239088 241078714 292474933 407965894 817187660157491604 406414444 462613431 308202469 221739029
992915760
In Sample Input 1, you can buy three copies of the $50 item using your $50, $51, and $100 gift certificate. You cannot buy a fourth copy of the $50 item using the $100 gift certificate since each gift certificate can be used to buy only one item. The $5 gift certificate cannot be used to buy anything.
Hint: there are several ways to solve this problem, but think about the big-$$$O$$$ before implementing. An $$$O(MN)$$$ solution is unlikely to fit in the time limit.
| Name |
|---|


