Compare the cheapest itinerary cost with and without a rail pass that discounts some fares, and decide if the pass is worth it.
Medium4Shortest pathGraphInterviewNo attempts yetTime limit1sMemory limit512 MBSeunghyeon and Jihak decided to travel inside Korea over summer vacation. Going abroad costs too much, so the two of them will tour several Korean cities instead. Korea has N cities, and Seunghyeon picked M of them as the itinerary, in visiting order. The route may pass through the same city more than once, and the city where the trip starts is the city where it ends.
K transport connections run between pairs of cities. The kinds are train, subway, bus, taxi and airplane, and the train kinds are Mugunghwa, ITX-Saemaeul, ITX-Cheongchun, KTX, S-Train and V-Train. Every connection has a fixed fare per person per ride. The two of them move through the M chosen cities in order at the lowest possible cost.
Korail also sells a rail pass called Nailro. During vacation a young traveller buys one for R won per person. With the pass, Mugunghwa, ITX-Saemaeul and ITX-Cheongchun rides are free for several days, and S-Train and V-Train rides cost 50% of the fare. KTX, the subway and every other kind get no discount.
Help Jihak work out which choice costs less, buying the pass or skipping it. Both travellers pay the same amount, so compare the cost for one person. The total with the pass is R plus the minimum travel cost computed from the discounted fares. The total without the pass is the minimum travel cost computed from the full fares. A 50% S-Train or V-Train fare is not always an integer, so compare the two totals exactly.
Print Yes on one line if buying the Nailro pass gives the smaller total cost, and No otherwise. Print No when the two totals are exactly equal.
In the first example the minimum cost with the Nailro pass is this.
Without the pass the minimum cost is this.
43,000 won is less than 43,500 won, so the answer is Yes.