#abc275h. Ex - Monster

Ex - Monster

Score : 600600 points

问题描述

在数轴上有 NN 个怪物。在坐标 ii (1iN)(1\leq i\leq N) 处有一个体力值为 AiA_i 的怪物。另外,在坐标 ii 处还有一个强度为 BiB_i 的永久护盾。

这个护盾即使在相同坐标上的怪物体力值为 00 或以下时也会持续存在。

Takahashi 是一位魔法师,他可以任意多次执行以下操作:

  • 选择满足 1lrN1\leq l\leq r\leq N 的整数 llrr
  • 然后,消耗 max(Bl,Bl+1,,Br)\max(B_l, B_{l+1}, \ldots, B_r) MP(魔法点数),以减少坐标 l,l+1,,rl,l+1,\ldots,r 上每个怪物的体力值各 11 点。

在选择 llrr 时,即使坐标 l,l+1,,rl,l+1,\ldots,r 上的部分怪物已经体力值为 00 或以下也是可以的。 但是请注意,所有这些坐标的护盾仍然存在。

Takahashi 想要使每个怪物的体力值变为 00 或以下。请找出实现这一目标所需的最小总魔法点数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN monsters along a number line. At the coordinate ii (1iN)(1\leq i\leq N) is a monster with a stamina of AiA_i.
Additionally, at the coordinate ii, there is a permanent shield of a strength BiB_i.
This shield persists even when the monster at the same coordinate has a health of 00 or below.

Takahashi, a magician, can perform the following operation any number of times.

  • Choose integers ll and rr satisfying 1lrN1\leq l\leq r\leq N.
  • Then, consume max(Bl,Bl+1,,Br)\max(B_l, B_{l+1}, \ldots, B_r) MP (magic points) to decrease by 11 the stamina of each of the monsters at the coordinates l,l+1,,rl,l+1,\ldots,r.

When choosing ll and rr, it is fine if some of the monsters at the coordinates l,l+1,,rl,l+1,\ldots,r already have a stamina of 00 or below.
Note, however, that the shields at all those coordinates still exist.

Takahashi wants to make the stamina of every monster 00 or below.
Find the minimum total MP needed to achieve his objective.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 1Ai,Bi1091 \leq A_i,B_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

B1B_1 B2B_2 \ldots BNB_N

Output

Print the minimum total MP needed to achieve his objective.

Sample Input 1

5
4 3 5 1 2
10 40 20 60 50

Sample Output 1

210

Takahashi can achieve his objective as follows.

  • Choose (l,r)=(1,5)(l,r)=(1,5). Consume max(10,40,20,60,50)=60\max(10,40,20,60,50)=60 MP, and the staminas of the monsters are (3,2,4,0,1)(3,2,4,0,1).
  • Choose (l,r)=(1,5)(l,r)=(1,5). Consume max(10,40,20,60,50)=60\max(10,40,20,60,50)=60 MP, and the staminas of the monsters are (2,1,3,1,0)(2,1,3,-1,0).
  • Choose (l,r)=(1,3)(l,r)=(1,3). Consume max(10,40,20)=40\max(10,40,20)=40 MP, and the staminas of the monsters are (1,0,2,1,0)(1,0,2,-1,0).
  • Choose (l,r)=(1,1)(l,r)=(1,1). Consume max(10)=10\max(10)=10 MP, and the staminas of the monsters are (0,0,2,1,0)(0,0,2,-1,0).
  • Choose (l,r)=(3,3)(l,r)=(3,3). Consume max(20)=20\max(20)=20 MP, and the staminas of the monsters are (0,0,1,1,0)(0,0,1,-1,0).
  • Choose (l,r)=(3,3)(l,r)=(3,3). Consume max(20)=20\max(20)=20 MP, and the staminas of the monsters are (0,0,0,1,0)(0,0,0,-1,0).

Here, he consumes a total of 60+60+40+10+20+20=21060+60+40+10+20+20=210 MP, which is the minimum possible.

Sample Input 2

1
1000000000
1000000000

Sample Output 2

1000000000000000000

Sample Input 3

10
522 4575 6426 9445 8772 81 3447 629 3497 7202
7775 4325 3982 4784 8417 2156 1932 5902 5728 8537

Sample Output 3

77917796

update @ 2024/3/10 11:36:16