#C2507. Minimum Steps to Collect All Coins

    ID: 45831 Type: Default 1000ms 256MiB

Minimum Steps to Collect All Coins

Minimum Steps to Collect All Coins

You are given the positions of coins in a grid. The player starts at \((0,0)\) and can only move right (increase \(x\)) or up (increase \(y\)). Your task is to compute the minimum number of steps required to collect all coins. You may collect the coins in any order, but since the player can only move right or up, the optimal strategy is to sort the coins in non-decreasing order of their \(x\) (and \(y\) when \(x\) values are equal) and then sum the differences between successive positions.

For example, to collect coins at positions \((1,2)\), \((2,3)\), and \((4,5)\), the minimum number of steps required is \(9\) because the player must move a total of \((1-0)+(2-0) = 3\) steps to reach \((1,2)\), then \((2-1)+(3-2) = 2\) steps to reach \((2,3)\), and finally \((4-2)+(5-3) = 4\) steps to reach \((4,5)\), adding up to \(3+2+4=9\).

inputFormat

The first line of input contains an integer \(n\) representing the number of coins. Each of the following \(n\) lines contains two space-separated integers \(x\) and \(y\) which denote the coordinates of a coin.

outputFormat

Output a single integer denoting the minimum number of steps required to collect all coins.

## sample
3
1 2
2 3
4 5
9

</p>