#P4974. Fastest Exit in the Castle

    ID: 18213 Type: Default 1000ms 256MiB

Fastest Exit in the Castle

Fastest Exit in the Castle

Viston has discovered a huge castle and wandered into a massive room. In the room, many adventurers have rushed into different corridors, each of which leads to a different room. In every room there is a teleportation portal that transports you to the next room. Eventually, all paths lead to the exit.

However, using a teleportation portal takes time. The time needed is determined by the number of people who have used that portal before. In particular, if a portal has already been used by M people, then the time cost to use that portal is given by $$T=M,$$ where the formula is in \(\LaTeX\) format.

Note that entering the initial room through a corridor does not consume any time; only the use of teleportation portals (inside the rooms) does.

Viston recalls, for each corridor from the outside, the sequence of waiting times (i.e. the number of people that previously used the portal in that room) encountered along the path from that corridor's initial room to the exit. Each corridor is described by a sequence of nonnegative integers ending with a 0, where the numbers before 0 represent the waiting times at successive teleportation portals. The 0 marks the exit (and does not count as waiting time).

Your task is to help Viston determine which corridor to choose from outside (using 1-indexing) so that his total waiting time is minimized. If there are multiple corridors with the same minimum time, output the one with the smallest index.

inputFormat

The first line contains an integer n (n ≥ 1), which is the number of corridors.

Each of the following n lines describes a corridor by a sequence of space‐separated nonnegative integers. In each sequence, the numbers before the terminating 0 represent the waiting times (in time units) for using the teleportation portals in successive rooms. The sequence always ends with a 0, which indicates the exit and does not add to the waiting time.

For example, a corridor described by "3 4 0" means the total waiting time along that corridor is 3 + 4 = 7.

outputFormat

Output two integers separated by a space: the index (1-indexed) of the corridor chosen, and the minimal total waiting time from that corridor.

sample

3
3 4 0
2 5 0
1 1 1 0
3 3