Skyline Photo Codeforces Round #709 Div. 2 Solution

Skyline Photo solution codeforces

Alice is visiting New York City. To make the trip fun, Alice will take photos of the city skyline and give the set of photos as a present to Bob. However, she wants to find the set of photos with maximum beauty and she needs your help. Skyline Photo solution codeforces

There are nn buildings in the city, the ii-th of them has positive height hihi. All nn building heights in the city are different. In addition, each building has a beauty value bibi. Note that beauty can be positive or negative, as there are ugly buildings in the city too.

A set of photos consists of one or more photos of the buildings in the skyline. Each photo includes one or more buildings in the skyline that form a contiguous segment of indices. Each building needs to be in exactly one photo. This means that if a building does not appear in any photo, or if a building appears in more than one photo, the set of pictures is not valid. Skyline Photo solution codeforces

The beauty of a photo is equivalent to the beauty bibi of the shortest building in it. The total beauty of a set of photos is the sum of the beauty of all photos in it. Help Alice to find the maximum beauty a valid set of photos can have.Input

The first line contains an integer nn (1≤n≤3⋅1051≤n≤3⋅105), the number of buildings on the skyline.

The second line contains nn distinct integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤n1≤hi≤n). The ii-th number represents the height of building ii.

The third line contains nn integers b1,b2,…,bnb1,b2,…,bn (−109≤bi≤109−109≤bi≤109). The ii-th number represents the beauty of building ii.Output Skyline Photo solution codeforces

Print one number representing the maximum beauty Alice can achieve for a valid set of photos of the skyline.Examples

Input

5
1 2 3 5 4
1 5 3 2 4

output

15

input

5
1 4 3 2 5
-3 4 -10 2 7

output

10

input

2
2 1
-2 -3

output 

-3

input

10
4 7 3 2 5 1 9 10 6 8
-4 40 -46 -8 -16 4 -10 41 12 3

output

96

Note

In the first example, Alice can achieve maximum beauty by taking five photos, each one containing one building.

In the second example, Alice can achieve a maximum beauty of 1010 by taking four pictures: three just containing one building, on buildings 11, 22 and 55, each photo with beauty −3−3, 44 and 77 respectively, and another photo containing building 33 and 44, with beauty 22.

In the third example, Alice will just take one picture of the whole city.

In the fourth example, Alice can take the following pictures to achieve maximum beauty: photos with just one building on buildings 11, 22, 88, 99, and 1010, and a single photo of buildings 33, 44, 55, 66, and 77. Skyline Photo solution codeforces

Leave a Comment