#P1842. [USACO05NOV] 奶牛玩杂技

[USACO05NOV] 奶牛玩杂技

Description

Each cow has its own weight and strength. Cow ii has weight WiW_i and strength SiS_i.

When a cow has other cows standing on her, she gets squashed to some extent. We call this her "squashed index." For any cow, her squashed index equals the total weight of all cows stacked above her (not including herself) minus her strength.

After the cows are stacked in some order, their overall squashed index is the squashed index of the most squashed cow.

Your task is to help the cows find an ordering that minimizes the overall squashed index.

Input Format

The first line contains an integer NN.

The next NN lines each contain two integers WiW_i and SiS_i.

Output Format

Output a single integer, the minimal overall squashed index.

3
10 3
2 5
3 3
2

Hint

Constraints: For 100%100\% of the testdata, 1N5×1041 \le N \le 5\times 10^4, 1Wi1041 \le W_i \le 10^4, 1Si1091 \le S_i \le 10^9.

Translated by ChatGPT 5