B. 风与牧歌的城邦(mondstadt)

    Type: Default File IO: mondstadt 1000ms 512MiB

风与牧歌的城邦(mondstadt)

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

题目描述

给定序列 {ana_n},{bnb_n},保证 a1,a2,...,ana_1,a_2,...,a_n 两两不同。

对于 ∀1≤l≤r≤n∀1≤l≤r≤n,定义区间 [l,r][l,r] 的权值如下:设 ap=maxap=max{al,al+1,...,ara_l,a_{l+1},...,a_r},则 [l,r][l,r] 的权值为 bpb_p。

你需要将 1∼n1∼n 划分为若干段,求每一段的权值之和的最大值。

输入格式

从文件 mondstadt.inmondstadt.in 中读入数据。

第一行一个正整数 nn。

第二行 nn 个正整数 a1,a2,...,ana_1,a_2,...,a_n。

第三行 nn 个正整数 b1,b2,...,bnb_1,b_2,...,b_n。

输出格式

输出到文件 mondstadt.outmondstadt.out 中。

一行一个整数表示答案。

样例 1 输入

5
3 4 2 5 1
3 -4 0 2 -5

样例 1 输出

5

样例 2

见右侧文件下的 mondstadt2.inmondstadt2.in 与 mondstadt2.ansmondstadt2.ans。

数据范围与提示

对于所有测试数据,保证 1≤n≤5×105,1≤ai≤n,−109≤bi≤1091≤n≤5×10^5,1≤a_i≤n,−10^9≤b_i≤10^9,保证 a1,a2,...,ana_1,a_2,...,a_n 是 1∼n1∼n 的一个排列。

每个测试点的具体限制见下表:

测试点编号 n≤n≤
1∼21∼2 10
3∼63∼6 1000
7∼127∼12 5×1045×10^4
3∼203∼20 5×1055×10^5

NOIp4 模拟赛

Not Attended
Status
Done
Rule
IOI
Problem
4
Start at
2024-11-28 19:00
End at
2024-11-28 21:00
Duration
2 hour(s)
Host
Partic.
9