#P12633. [UOI 2020] Skyscraper

[UOI 2020] Skyscraper

题目描述

Vus the Cossack lives in a skyscraper.

Since he started working as a builder, nn customers gave him the task to build nn skyscrapers. One of them should be at a distance of one kilometer from the skyscraper of Vus the Cossack, the other should be at a distance of two kilometers, the other should be at a distance of three kilometers and so on. All skyscrapers (including Cossack) must be on the same line, with his skyscraper being the leftmost.

The ii-th customer wants his skyscraper to be aia_i high. However, customers do not care how far their skyscrapers will be from the Vus' skyscraper. Therefore, the Cossack can decide for himself in what order to build other skyscrapers from his skyscraper.

Vus the Cossack wants to make the view from his skyscraper as beautiful as possible. We will assume that the ii-th floor of a certain skyscraper is visible from the Mustache skyscraper only if there is no other skyscraper between them that will have the ii-th floor. Vus the Cossack believes that every floor of the ii-th skyscraper has the beauty of bib_i. Therefore, he wants the total beauty of all the floors he will see from his skyscraper to be as great as possible.

An example for nn = 4.

The figure shows an example for n=4n=4. At a distance of one kilometer built a two-story skyscraper with the beauty of 44. Then one-story with the beauty of 22. Then a three-story with the beauty of 11. Finally, the latter built a 44-story skyscraper with a 33 beauty. From the Vus' skyscraper you can see only both floors of the first skyscraper, the third floor of the third and the fourth floor of the fourth skyscraper. Accordingly, we consider the total beauty of these floors: 4+4+1+3=124+4+1+3=12. Note that this may not be the optimal order of construction.

Help him find the maximum possible beauty.

输入格式

The first line contains one integer nn (1n1051 \leq n \leq 10^5) --- the number of skyscrapers that need to be built.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1091 \leq a_i \leq 10^9) --- the height of skyscrapers.

The third line contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n (1bi1091 \leq b_i \leq 10^9) --- the beauty of the floors in the skyscrapers.

输出格式

Print one integer --- the maximum possible total beauty.

4
2 1 3 4
4 2 1 3
14
6
1 10 3 9 8 2
8 3 2 4 5 6
51

提示

  • (1010 points) $1 \leq n \leq 10, 1 \leq a_i \leq 10, 1 \leq b_i \leq 10$;
  • (2727 points) $1 \leq n \leq 10^3, 1 \leq a_i \leq 10^3, 1 \leq b_i \leq 10^3$;
  • (2525 points) $1 \leq n \leq 10^3, 1 \leq a_i \leq 10^9, 1 \leq b_i \leq 10^9$;
  • (3838 points) without additional restrictions.