#C3346. Taco: The Minimum Horizontal Flight Challenge

    ID: 46763 Type: Default 1000ms 256MiB

Taco: The Minimum Horizontal Flight Challenge

Taco: The Minimum Horizontal Flight Challenge

You are given a series of datasets. In each dataset, the first line contains an integer n representing the number of points. The following n lines each contain two integers representing the x and y coordinates of points.

The bird starts from the first coordinate given. At every step, the bird can only move to a point that is strictly higher than its current altitude. Among all such options, the bird always chooses the point with the smallest x coordinate. The process repeats until the bird reaches the highest peak (i.e. the point with the maximum y‐coordinate). Your task is to determine the total horizontal distance the bird flies in order to reach the highest peak.

Formally, let the starting position be P0 and let H be the point with the maximum altitude. The bird performs a series of hops from one point to another via points of increasing altitude, and the flight cost is the sum of horizontal distances between successive points. Output the computed total horizontal distance.

Note: Each dataset is processed independently. The input terminates with a single line containing "0" which should not be processed.

inputFormat

The input consists of multiple datasets. For each dataset:

  • The first line contains an integer n (n ≥ 2) indicating the number of points.
  • The next n lines each contain two space-separated integers x and y, representing the coordinates of a point.

The input ends with a line containing 0.

outputFormat

For each dataset, output a single line containing an integer denoting the minimum total horizontal distance the bird has to travel to reach the highest peak.

## sample
5
0 0
3 1
6 5
9 8
12 4
3
0 2
3 6
6 10
4
0 0
2 3
4 7
8 10
0
9

6 8

</p>