#P7667. Art Exhibition Selection

    ID: 20857 Type: Default 1000ms 256MiB

Art Exhibition Selection

Art Exhibition Selection

There are \(N\) art pieces that are candidates for an exhibition. The art pieces are numbered from \(1\) to \(N\). Each art piece \(i\) has a size \(A_i\) and a value \(B_i\). At least one art piece must be selected for the exhibition. Although there is enough space to display all \(N\) art pieces, due to the aesthetic tastes of the JOI Republic citizens, the size differences among the selected art pieces should not be too large. On the other hand, we want to display many high-value art pieces.

We define the following for a chosen set of art pieces:

  • Let \(A_{\text{max}}\) be the maximum size among the selected pieces.
  • Let \(A_{\text{min}}\) be the minimum size among the selected pieces.
  • Let \(S\) be the sum of the values \(B_i\) of the selected pieces.

The objective is to maximize the expression:

[ S - \left(A_{\text{max}} - A_{\text{min}}\right) ]

Given the number of art pieces and the size and value for each, compute the maximum achievable value of \(S - (A_{\text{max}} - A_{\text{min}})\) by selecting a non-empty subset of the art pieces.

inputFormat

The first line contains an integer (N) ((1 \leq N \leq 10^5)), representing the number of art pieces. Each of the following (N) lines contains two integers (A_i) and (B_i) ((1 \leq A_i, B_i \leq 10^9)), which represent the size and value of the (i)-th art piece, respectively.

outputFormat

Output a single integer: the maximum value of (S - (A_{\text{max}} - A_{\text{min}})) achievable by selecting a non-empty subset of art pieces.

sample

3
5 6
2 1
8 10
16

</p>