## Inaho III

View as PDF

Points: 17 (partial)
Time limit: 5.0s
Memory limit: 512M

Authors:
Problem types

Now that Inaho knows the direction of the least density, he wants to get out of this horrible -dimensional hole to return to playing his Graph Simulator 2015. Each unit of density costs him year to walk through, and he wants to leave as quickly as possible. Of course, these are -dimensional years we're talking about, which translates to nanosecond in -dimensional space. As he still cannot understand the complexities of dimensions, he asks for your help once again!

Inaho wants you to find the minimum time he needs to travel in order to escape, if he is only able to travel parallel to any axis. He is currently at the position of the greatest density, and would like to get to the position of the least density.

Inaho must wade out of his starting point, and wade into the position of the least density. Therefore the time taken includes the time needed to travel out of the starting point and into the destination point.

#### Input Specification

The first line will contain the integer , the number of dimensions.

The second line will contain space-separated integers that represent the size of each dimension, .

On the third line will contain integers .

represents the density at position where .

In other words, the third line will be a flattened -dimensional grid.

If there are multiple positions with the least density, Inaho's destination is at the position that occurs the earliest in the input.

If there are multiple positions with the greatest density, Inaho is at the position that occurs the latest in the input.

It is guaranteed that any simple path through this -dimensional space take no longer than years.

#### Output Specification

On the first line, output the minimum amount of time he needs to travel in this -dimensional hole before he can reach the position of least density.

On the subsequent lines, output the points Inaho must visit to reach the position of least density (including the source and the destination) in order. Each line shall consist of -indexed space separated integers specifying Inaho's position in each dimension.

Printing out any valid simple path with the shortest distance will grant full marks.

For only printing the shortest distance without the path, of points will be granted.

For 1 of the 17 available marks, .

For an additional 1 of the 17 available marks, .

For an additional 1 of the 17 available marks, .

For an additional 2 of the 17 available marks, .

For an additional 3 of the 17 available marks, .

#### Sample Input 1

2
5 5
14 4 15 1 7 15 6 9 3 13 13 6 8 13 4 11 11 1 14 10 11 2 15 9 10

#### Sample Output 1

37
4 2
3 2
2 2
1 2
1 3
0 3

#### Sample Input 2

2
20 2
10 6 1 13 1 15 2 10 12 3 3 5 10 8 3 1 2 1 6 2 4 6 13 1 3 9 7 15 16 5 4 7 1 16 6 4 6 7 10 5

#### Sample Output 2

93
16 1
15 1
14 1
13 1
12 1
11 1
10 1
9 1
8 1
7 1
6 1
5 1
4 1
3 1
3 0
2 0
1 0