Nine

Time limit1sMemory limit128 MB

Summary
For each desired microwave time, pick the four-digit MM:SS entry with the most 9s, then the least error under 10%, then lexicographically smallest.
Level

Medium4 of 10

Topics
Brute force, Implementation, Simulation, Sorting
Solved
No attempts yet

Problem

Randall Munroe of xkcd.com observed that 9 is the most rarely used key on a microwave keypad. Let's spread the load.

A microwave time is entered as four digit keys, displayed as MM:SS. The first two digits are the minutes field and the last two are the seconds field; each field may take any value from 00 to 99 (so the seconds field is allowed to exceed 59). The time in seconds represented by an entry MM:SS is 60×MM+SS60 \times MM + SS.

Given a desired cooking time TT (in seconds), find the four-key entry whose represented time T9T_9 uses as many 9 keys as possible while keeping the error below 10%. Formally, an entry is allowed only when

10 ∣T−T9∣<T.10\,|T - T_9| < T.

Among all allowed entries, choose the one that uses the greatest number of 9 digits. Break ties by the smallest error ∣T−T9∣|T - T_9|, and break any remaining ties by choosing the lexicographically smallest MM:SS string.

Worked example: for T=T = 01:15 (that is, T=75T = 75 seconds), every time from 6868 to 8282 seconds has less than 10% error. Among those, 00:69, 00:79, 01:09, and 01:19 use the most 9 keys (one each). The ones with the smallest error are 00:79 and 01:19 (error 44), and the lexicographically smaller of those is 00:79.

Input

The input contains several test cases. Each line holds one desired cooking time in MM:SS format, where every digit is between 0 and 9. The line 00:00 marks the end of the input and is not processed.

Output

For each test case, print on its own line the four keys to press, in MM:SS format.

Examples2

  1. Example 1

    Input
    00:30
    01:00
    02:00
    91:30
    46:03
    00:00
    
    Expected output
    00:29
    00:59
    01:59
    90:99
    49:99
    
  2. Example 2

    Input
    01:15
    00:00
    
    Expected output
    00:79