At the end of this process, say person
Constraints
Subtask 1 [20%]
Subtask 2 [30%]
Subtask 3 [50%]
Input Specification
The first line contains a single integer
The next
Output Specification
Output a single space-separated integer, the minimum possible unhappiness.
Sample Input
4
15 4
3 18
10 3
8 2
Sample Output
17
Explanation for Sample
The first person trades his hat with the second person in line and leaves with a hat of value
Comments