Under construction

Still being built. Things may change

Road Trip

Interview

Time limit1sMemory limit128 MB

Summary
Buy and resell fuel at each town's price within tank capacity to complete every leg of each journey at minimum cost.
Level

Medium5 of 10

Topics
Greedy
Solved
No attempts yet

Problem

You are planning a road trip to visit friends who live in different towns. You do not want to spend more on fuel than you have to, but every town sells fuel at its own price, so the trip needs careful planning. Taking on extra fuel where it is cheap means buying less where it is expensive, and at some towns selling surplus fuel back is the better move. The tank holds only so much, and at every town you must take on enough fuel to reach the next one. Arriving with an empty tank is fine.

Write a program that plans the trip.

Input

The input describes a series of journeys. Each journey begins with a line holding the capacity of the fuel tank cc in litres (0<c<1000 < c < 100) and the number of towns to visit tt (0<t<200 < t < 20). A line containing two zeros ends the input.

The next tt lines describe the successive stages of that journey. Each line holds the price pp of one litre of fuel in the town at the start of the stage, written in fixed point dollars and cents with 0.01≤p<9.990.01 \le p < 9.99, and the number of litres nn needed to reach the next town, an integer with 1≤n<1001 \le n < 100. Buying and selling use the same price.

Every stage is reachable, so nn never exceeds cc.

Output

Print one line per journey. Write the journey number in the form Journey k:, then a single space, then the minimum cost of completing that journey as a fixed point number with two decimal places. Here kk is the journey number, counting from 1.

Examples1

  1. Example 1

    Input
    10 3
    2.00 7
    1.50 8
    1.00 3
    50 6
    1.50 20
    4.20 5
    1.15 35
    1.41 27
    1.92 30
    2.21 15
    0 0
    
    Expected output
    Journey 1: 29.00
    Journey 2: 117.64