CF1483C.Skyline Photo

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

There are nn buildings in the city, the ii -th of them has positive height hih_i . All nn building heights in the city are different. In addition, each building has a beauty value bib_i . 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.

The beauty of a photo is equivalent to the beauty bib_i 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.

输入格式

The first line contains an integer nn ( 1n31051 \le n \le 3 \cdot 10^5 ), the number of buildings on the skyline.

The second line contains nn distinct integers h1,h2,,hnh_1, h_2, \ldots, h_n ( 1hin1 \le h_i \le n ). The ii -th number represents the height of building ii .

The third line contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n ( 109bi109-10^9 \le b_i \le 10^9 ). The ii -th number represents the beauty of building ii .

输出格式

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

输入输出样例

  • 输入#1

    5
    1 2 3 5 4
    1 5 3 2 4

    输出#1

    15
  • 输入#2

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

    输出#2

    10
  • 输入#3

    2
    2 1
    -2 -3

    输出#3

    -3
  • 输入#4

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

    输出#4

    96

说明/提示

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 .

首页