Seesaw

시간 제한2초메모리 제한2048 MB

요약
수직선 위에 순서대로 놓인 사람들을 순서를 유지한 채 최소한으로 움직여 위치와 무게의 곱의 합이 0이 되도록 만든다.
난이도

어려움10점 중 9점

유형
수학, 그리디, 누적 합, 분할 정복
정답자
아직 제출이 없습니다

문제

A number of people are sitting on an infinitely long seesaw. The seesaw can be represented as a number line centered at 00. 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 00. 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?

입력

The first line of input contains a single integer nn (1≤n≤1051\leq n\leq 10^5), which is the number of people.

Each of the next nn lines contains two integers pp (−108≤p≤108-10^8 \leq p \leq 10^8) and ww (1≤w≤1051\leq w \leq 10^5), where pp is that person’s position on the seesaw, and ww is that person’s weight. The values of pp are guaranteed to be unique and in ascending order.

출력

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−610^{-6}.

예제2

  1. 예제 1

    입력
    3
    -3 4
    3 1
    5 1
    
    예상 출력
    1.000000
    
  2. 예제 2

    입력
    3
    -2 1
    1 4
    2 4
    
    예상 출력
    2.500000