N is at most 10, so enumerate subsets of bands and find the cheapest subset whose summed intervals contain L and whose total price is within M.
Medium4Brute forceArrayIntervalsDynamic programmingNo attempts yetTime limit5sMemory limit512 MBMary likes playing with rubber bands. Today is her birthday, so you went to the rubber band shop to buy her a gift.
The shop has N rubber bands. The i-th band can be stretched to any length from Ai to Bi, both ends included. Connecting a band with range [a,b] to a band with range [c,d] makes one band with range [a+c,b+d]. A band made that way can be connected to other bands again.
You want to give Mary a band that can be stretched to a length of exactly L. It can be a single band or a band built by connecting several of them. You have M dollars. Find the smallest amount you can spend. If the goal cannot be reached, print IMPOSSIBLE.
The first line holds the number of test cases, T. T test cases follow.
The first line of each test case holds three integers N, M, L: the number of rubber bands in the shop, the amount of money you have, and the desired length. Each of the next N lines describes one rubber band with three integers Ai, Bi, Pi. [Ai,Bi] is the range of lengths the i-th band can stretch to, both ends included, and Pi is its price in dollars.
For each test case, print one line in the form Case #x: y. Here x is the test case number, starting from 1. y is IMPOSSIBLE if you cannot buy rubber bands that meet the goal, and otherwise the minimum price you have to pay.
In the first test case of the sample, no single band reaches length 6. Buying the two cheapest bands and connecting them gives the range [7,9], which does not contain 6. The band must stretch to a length of exactly L. Connecting the band that costs 2 with the band that costs 5 gives the range [4,7], which does contain 6, and the total of 7 dollars fits within the 8 dollars you have.
In the second test case, reaching length 14 needs all three bands, which cost 12 dollars together. You only have 11 dollars, so the answer is IMPOSSIBLE.