Potatoes and fertilizers | 프로그래밍의 벗 PivotOJ
PivotOJ

Potatoes and fertilizers

시간 제한: 1000ms메모리 제한: 1024MB출처: LMIO 2018-2019BOJ 27345

문제

Farmer Gumbauskas is growing potatoes. He planted potatoes in one long furrow and placed bags with fertilisers next to the furrow.

Assume that the furrow consists of NN segments of the same length. The segments are numbered from 11 to NN from left to right. In segment ii there are aia_i fertilisers and were planted bib_i potatoes. One fertiliser unit is required to fertilise one planted potato. There is enough fertiliser for all the potatoes, i.e. a_1 + \cdots + a_N ≥ b_1 + \cdots + b_N.

However, it costs to transfer fertiliser from one segment to another. To transfer one unit of fertiliser from segment ii to segment jj costs ij|i - j|.

Find the cheapest way to fertilise all the potatoes.

입력

The length of the furrow NN is given in the first line.

Each of the remaining NN lines contain two integers aia_i and bib_i – the amount of fertiliser unit and the amount of potatoes planted in segment ii. The segments are given in the increasing order of ii.

출력

Output the smallest possible cost of fertilising all the planted potatoes.

예제

예제 1

입력
6
1 2
0 0
2 0
0 0
0 0
0 1
출력
5

예제 2

입력
7
2 0
2 0
2 0
0 5
2 0
2 0
2 0
출력
6
코드를 제출하려면 로그인하세요.