Now that Inaho knows the direction of the least density, he wants to get out of this horrible
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 second line will contain
On the third line will contain
In other words, the third line will be a flattened
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
Output Specification
On the first line, output the minimum amount of time he needs to travel in this
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
Printing out any valid simple path with the shortest distance will grant full marks.
For only printing the shortest distance without the path,
Subtasks
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
Comments