This page is still under construction.

Parts of this page are still being built. What you see may change.

Two Sawmills

Time limit1sMemory limit128 MB

Summary
Place two extra sawmills along a road so that every tree's downhill haul to the first mill at or below it is minimized.
Level

Medium7 of 10

Topics
Dynamic programming, Divide and conquer, Prefix sum, Greedy
Solved
No attempts yet

Problem

nn old trees stand along a road that runs from the top of a hill down to its foot. All of them will be cut down, and to avoid wasting wood every felled tree must be carried to a sawmill.

Wood can be moved in one direction only: downhill. There is already a sawmill at the lower end of the road. You may build two more sawmills at points along the road, and you must choose their locations so that the total transportation cost is as small as possible. Each felled tree travels downhill to the first sawmill at or below its own position. Transportation costs one cent per kilogram of wood per meter.

Given the number of trees together with their weights and positions on the standard input, write a program that computes the minimum possible total transportation cost and prints it to the standard output.

Input

The first line contains the number of trees nn (2≤n≤200002 \le n \le 20000). The trees are numbered 1,2,…,n1, 2, \dots, n from the top of the hill downwards. Each of the next nn lines contains two integers separated by a single space. Line ii contains wiw_i, the weight in kilograms of tree ii (1≤wi≤100001 \le w_i \le 10000), and did_i, the distance in meters between tree ii and tree i+1i+1 (0≤di≤100000 \le d_i \le 10000). The last of these distances, dnd_n, is the distance from tree nn to the sawmill at the lower end of the road. It is guaranteed that the total cost of carrying every tree to the sawmill at the end of the road is less than 2,000,000,000 cents.

Output

Print a single integer: the minimum total transportation cost.

Hint

The two extra sawmills may be placed at tree positions. Every tree is carried to the nearest sawmill at or below its own position. The figure below shows an optimal placement of the sawmills for the first test case; trees are drawn as circles labeled with their weights, and sawmills are marked in black. The resulting minimum cost is

1⋅(2+1)+2⋅1+1⋅(1+2)+3⋅2+2⋅(1+2+1)+1⋅(2+1)+1⋅1=26.1 \cdot (2 + 1) + 2 \cdot 1 + 1 \cdot (1 + 2) + 3 \cdot 2 + 2 \cdot (1 + 2 + 1) + 1 \cdot (2 + 1) + 1 \cdot 1 = 26.

Examples1

  1. Example 1

    Input
    9
    1 2
    2 1
    3 3
    1 1
    3 2
    1 6
    2 1
    1 2
    1 1
    
    Expected output
    26