Problem H
Seesaw
A number of people are sitting on an infinitely long seesaw. The seesaw can be represented as a number line centered at $0$. Each person has a weight, and sits at a location along the seesaw. They contribute a torque equal to their weight times their position. The seesaw is balanced if the sum of torques is $0$. People are able to move any real-valued distance along the seesaw, so long as they do not go past the person immediately before them or after them. In other words, the relative ordering of the people along the seesaw must be preserved. It is ok for multiple people to occupy the same location, and for a person to move multiple times. What is the minimum sum of distances that people have to move to make the seesaw balanced?
Input
The first line of input contains a single integer $n$ ($1\leq n\leq 10^5$), which is the number of people.
Each of the next $n$ lines contains two integers $p$ ($-10^8 \leq p \leq 10^8$) and $w$ ($1\leq w \leq 10^5$), where $p$ is that person’s position on the seesaw, and $w$ is that person’s weight. The values of $p$ are guaranteed to be unique and in ascending order.
Output
Output a single number, which is the minimum total amount of distance moved for all people in order to balance the seesaw. Answer is correct if it is within absolute or relative error of $10^{-6}$.
Sample Input 1 | Sample Output 1 |
---|---|
3 -3 4 3 1 5 1 |
1.000000 |
Sample Input 2 | Sample Output 2 |
---|---|
3 -2 1 1 4 2 4 |
2.500000 |