#P7667. [JOI 2018 Final] 美术展览 / Art Exhibition

[JOI 2018 Final] 美术展览 / Art Exhibition

Description

There are NN artworks that are candidates for the exhibition. The artworks are numbered from 11 to NN. Each artwork is defined by two integers: size and value. Artwork ii (1iN1 \leq i \leq N) has size AiA_i, and artwork ii has value BiB_i.

In the exhibition, at least one artwork will be selected and displayed. Since the exhibition hall is large enough, it is possible to display all NN artworks. However, due to the aesthetic sense of the people of the JOI Republic, we want the selected artworks to have sizes that do not differ too much. On the other hand, we also want to display many high-value artworks. We decide to select the artworks for the exhibition according to the following rules:

  • Among the selected artworks, let AmaxA_{max} be the maximum size, and let AminA_{min} be the minimum size. Let SS be the total value of the selected artworks.
  • Then, we want to maximize S(AmaxAmin)S-(A_{max}-A_{min}).

Given the number of candidate artworks for the exhibition, and the size and value of each artwork, write a program to compute the maximum value of S(AmaxAmin)S-(A_{max}-A_{min}).

Input Format

The first line contains an integer NN, the number of candidate artworks for the exhibition. Each of the next NN lines, the ii-th line contains two space-separated integers AiA_i and BiB_i, meaning that artwork ii has size AiA_i and value BiB_i.

Output Format

The only line contains one integer, the maximum value of S(AmaxAmin)S-(A_{max}-A_{min}).

3
2 3
11 2
4 5
6
6
4 1
1 5
10 3
9 1
4 2
5 3
7
15 
1543361732 260774320
2089759661 257198921
1555665663 389548466
4133306295 296394520
2596448427 301103944
1701413087 274491541
2347488426 912791996
2133012079 444074242
2659886224 656957044
1345396764 259870638
2671164286 233246973
2791812672 585862344
2996614635 91065315
971304780 488995617
1523452673 988137562
4232545716

Hint

Constraints

For 100%100\% of the testdata, 2N5×1052 \leq N \leq 5 \times 10^5, 1Ai10151 \leq A_i \leq 10^{15} (1iN1 \leq i \leq N), 1Bi1091 \leq B_i \leq 10^9 (1iN1 \leq i \leq N).

  • Subtask 11 (1010 points): N16N \leq 16.
  • Subtask 11 (2020 points): N300N \leq 300.
  • Subtask 22 (2020 points): N5000N \leq 5000.
  • Subtask 33 (5050 points): No additional constraints.

Sample Explanation

For Sample 11: There are 33 candidate artworks in this exhibition. The size and value of each artwork are as follows:

  • Artwork 11 has size 22 and value 33.
  • Artwork 22 has size 1111 and value 22.
  • Artwork 33 has size 44 and value 55.

In this case, if we choose artwork 11 and artwork 33 for the exhibition, we have S(AmaxAmin)S-(A_{max}-A_{min}) as follows:

  • Among the selected artworks, artwork 33 has the largest size. Therefore, Amax=4A_{max}=4.
  • Among the selected artworks, artwork 11 has the smallest size. Therefore, Amin=2A_{min}=2.
  • The total value of the selected artworks is 3+5=83+5=8. Therefore, S=8S=8.

Since S(AmaxAmin)S-(A_{max}-A_{min}) cannot be greater than 77, output 66.

Problem Notes

This problem is from T2: Art Exhibition of The 17th Japanese Olympiad in Informatics (JOI 2017/2018) Final Round.
Translated and organized by @求学的企鹅.

Translated by ChatGPT 5