#P2647. 最大收益

最大收益

Description

You are given nn items, numbered 1,2,3,,n1, 2, 3, \cdots, n. You may choose any subset of them in any order. The ii-th item has two attributes WiW_i and RiR_i. When you choose the ii-th item, you immediately gain a profit of WiW_i; however, the profits of all items chosen after this item are reduced by RiR_i. Determine which items to choose and in what order to choose them so that the total profit is maximized.

Note that the reductions are cumulative. For example, if you choose item ii, you gain WiW_i; then if you choose item jj, you gain WjRiW_j - R_i; afterwards, if you choose item kk, you gain WkRiRjW_k - R_i - R_j; hence the total profit is Wi+(WjRi)+(WkRiRj)W_i + (W_j - R_i) + (W_k - R_i - R_j).

Input Format

The first line contains a positive integer nn, the number of items.

Lines 22 through n+1n+1 each contain two non-negative integers WiW_i and RiR_i, as described above.

Output Format

Output a single line with the maximum profit.

2
5 2
3 5
6

Hint

Constraints

  • 20% of the testdata satisfies n5n \le 5, 0Wi,Ri10000 \le W_i, R_i \le 1000.
  • 50% of the testdata satisfies n15n \le 15, 0Wi,Ri10000 \le W_i, R_i \le 1000.
  • 100% of the testdata satisfies n3000n \le 3000, 0Wi,Ri2×1050 \le W_i, R_i \le 2 \times 10^5.

Sample Explanation

We can choose item 11 to gain 55 profit; then choose item 22 to gain 32=13 - 2 = 1 profit. The total profit is 5+1=65 + 1 = 6.

Translated by ChatGPT 5