TTBRM - To the Bird-planet
"Weddings are basically funerals with cake Morty"
Birdperson has invited Rick and Morty to attend his marriage party. Rick doesn't like to go to marriage parties but Morty wants to go, as Birdperson is a very close friend. Also, Birdperson is king of Bird-planet and wants to compensate for the travel expenses for Rick's family. Bird-planet is many light years away from the earth but that is not the problem as Rick has Space Cruiser with hyperspeed.
Space cruiser needs 1 unit of fuel to travel 1 light year but it has a limited capacity 'C' so they may need to refuel in-between. There are many planets between Birdplanet and Earth and the fuel pricing is different on each planet.
Let Earth is numbered as '1' and bird-planet as 'N+1' then they can fuel at n planets (including earth). Distance between any two consecutive planets is 'D' light-years.
Evil Rick wants to maximize the expense of fueling and Birdperson knows this so he put a condition that on arrival there should be zero fuel in the tank.
Your task is to maximize the expense and it is obvious that they can't fuel at bird-planet. Initially, there is zero fuel in the tank.
The first line contains T, the number of test cases. Then the test cases follow.
The first line of each test case contains N, C and D, Number of planets for fueling (including earth), Capacity of the fuel tank and distance between two consecutive planets
Second-line contains A1 A2 A3… AN, where Ai denotes the price of 1 unit of fuel on ith planet.
For each test case print the maximum possible expense.
3 5 3
1 5 2
-As N=3 so birdplanet will be 4th planet and hence they will need to travel 3*3=9 light year,
-first they will buy 3 unit fuel from 1st planet to go to 2nd planet, expense: 3*1=3
-They will buy 5 unit of fuel (up to full capacity) at 2nd planet, expense: 5*5=25
-There will be 2 unit of fuel remaining on reaching 3rd planet so the need to buy only 1 more unit of fuel to reach Birdplanet, expense: 1*2=2
-Total expense 3+25+2=30
A great problem to learn greedy algorithms from.
Sample tricky test cases are given below:
Can you please see my latest solution, and just let me know if many cases are getting WA? No other hints needed
Thanks for pointing out @devilwolverine but there are no such cases. The answer will always fit in long long range
what if N is 10^5 and D is 10^5 and Ai is 10^9 for all i, I think answer won't fit in long long, are there any such cases?
Last edit: 2020-01-13 21:56:30
Thanks for the compliment :D :)
Very nice problem. Description is also fun. This must be the only problem where maximizing cost makes sense :)