Airport Coffee
Time limit6sMemory limit512 MB
Given spaced coffee carts along a corridor, choose where to buy cups so the total walking time with alternating slow and fast phases is minimized, output as a fraction.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Prefix sum, Math
- Solved
- No attempts yet
Problem
Jonna flies to programming contests. She lives in Helsinki, so she usually has to reach a large hub first, for example Copenhagen Airport, and take a connecting flight from there. Flights run late all the time, and a late flight hurts most when a connection is waiting.
Jonna has just landed at Copenhagen Airport and has to catch her connection to Heathrow Airport. Her flight from Helsinki was delayed, so she has to walk quickly from the arrival gate to the departure gate. Jonna normally walks centimeters per second. There is one complication: she is slightly addicted to coffee, and she drags her feet when she is not drinking any. The coffee itself does nothing for her legs, but the grumpiness of not drinking it beats even the fear of a missed flight. While she is drinking coffee she walks centimeters per second.
The arrival gate and the departure gate are centimeters apart, and small coffee carts stand along the way. Paying with a contactless card is instant, so buying a cup takes no time, but the coffee is too hot to drink right away. For seconds after the purchase Jonna waits for the cup to cool down and keeps walking at the slow speed. Exactly seconds after the purchase she starts drinking, and emptying the cup takes exactly seconds, during which she walks at the fast speed. Once the cup is empty she walks slowly again.
Jonna carries a bag in her left hand, so she can hold only one cup at a time. She may throw away a cup that still contains coffee and buy a fresh one, wasteful as that is.
Jonna never stops walking, and she can buy coffee only at a cart, at the moment she passes it. Find the shortest time she needs to reach the departure gate.
Input
The first line contains five integers , , , and .
- is the distance between the arrival gate and the departure gate in centimeters.
- are Jonna's walking speeds in centimeters per second, first while she is not drinking coffee and then while she is drinking coffee.
- is the number of seconds she has to wait before she can drink a cup.
- is the number of seconds it takes her to empty a cup.
The second line contains one integer , the number of coffee carts between the two gates ().
The third line contains integers, the positions of the coffee carts as distances from the arrival gate in centimeters, in ascending order. Every position is between and inclusive, and no two carts share a position. The third line is empty when .
Output
Print the shortest time in seconds that Jonna needs to reach the departure gate. That time is always a rational number, so print it as the irreducible fraction , where and the greatest common divisor of and is . When the time is a whole number of seconds, is , so a time of 40 seconds is printed as 40/1.
Note

The figure illustrates the first example. The carts where Jonna buys coffee are marked with triangles, and the dotted parts are the stretches she walks faster because she is drinking. She buys at the carts and centimeters from the arrival gate. The first cup cools down centimeters from the start, the second one centimeters from the start. She walks of the centimeters while drinking, so the total time is seconds. Buying at the carts and takes exactly as long, and the printed answer is the same.