Clocks
Time limit1sMemory limit128 MB
Pick one time every clock can show so the total forward movement from the current readings is as small as possible.
- Level
Medium6 of 10
- Topics
- Sorting, Prefix sum, Math
- Solved
- No attempts yet
Problem
In Byteland lives a clockmaker named Gustaw, who has repaired clocks for many years. His workshop is full of old clocks, and every one of them is an analog clock that shows the time with hands. Each clock has two hands: a byte-hour hand and a byte-minute hand, and both hands only ever move forward (clockwise).
In Byteland one byte-hour always lasts exactly byte-minutes, so every clock has a minute scale divided into byte-minutes. However, the length of a day in Byteland has changed many times over the years, so the clocks do not all have the same number of byte-hours on their dials. The dial of clock is divided into byte-hours, numbered through .
The king is about to visit, and Gustaw wants to make the best possible impression, so he decides that every clock should show the exact same time. Right now every clock is stopped. Because the clocks are very old and could break, Gustaw must not turn the hands by hand. Instead he may pick any single clock, start it, let it run forward by as many byte-hours and byte-minutes as he likes, and then stop it. He can never run two clocks at the same time, so he must set them one after another, and the times he waits for each clock all add up.
Gustaw picks one target time that every clock is able to display: a whole byte-hour together with a byte-minute (with ). Because the dial of clock only carries the byte-hours , the target byte-hour must satisfy for every clock. He then runs each clock forward from its current reading to that target time. Since the hands only move forward, a clock whose current position is byte-minutes (where ) must run forward by byte-minutes.
Help Gustaw choose the target byte-hour and byte-minute so that the total time he spends running the clocks is as small as possible, and report that minimum total time.
Input
The first line contains one integer (), the number of clocks. Each of the next lines describes one clock with three integers , , : the byte-hour currently shown by clock , the byte-minute currently shown, and the number of byte-hours on that clock's dial ( and ).
Output
Print one line with two integers: the number of byte-hours and the number of byte-minutes that Gustaw must wait in total when he uses the smallest possible total time. (A total of byte-minutes is written as byte-hours and byte-minutes.)