F - Happy Birthday! 3 Editorial /

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 550550

問題文

半径で切って NN 等分された円形のケーキがあります。

各ピースには時計回りに 1,2,,N1, 2, \ldots, N の番号が付けられています。また、1iN1 \leq i \leq N なる整数 ii についてピース ii をピース N+iN + i とも呼ぶこととします。

はじめ、すべてのピースの色は色 00 です。

あなたは、以下の操作を好きな回数行うことができます。

  • 1a,b,cN1 \leq a, b, c \leq N なる整数 a,b,ca, b, c を選ぶ。0i<b0 \leq i < b なる各整数 ii に対してピース a+ia + i の色を色 cc に変更する。この操作にはコストが b+Xcb + X_c かかる。

1iN1 \leq i \leq N なるすべての整数 ii に対してピース ii の色を色 CiC_i にするために必要なコストの合計の最小値を求めてください。

制約

  • 1N4001 \leq N \leq 400
  • 1CiN1 \leq C_i \leq N
  • 1Xi1091 \leq X_i \leq 10^9
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN
C1C_1 C2C_2 \ldots CNC_N
X1X_1 X2X_2 \ldots XNX_N

出力

答えを出力せよ。


入力例 1Copy

Copy
6
1 4 2 1 2 5
1 2 3 4 5 6

出力例 1Copy

Copy
20

ピース ii の色が色 AiA_i であるとします。はじめ、(A1,A2,A3,A4,A5,A6)=(0,0,0,0,0,0)(A_1, A_2, A_3, A_4, A_5, A_6) = (0, 0, 0, 0, 0, 0) です。

(a,b,c)=(2,1,4)(a, b, c) = (2, 1, 4) として操作を行うと (A1,A2,A3,A4,A5,A6)=(0,4,0,0,0,0)(A_1, A_2, A_3, A_4, A_5, A_6) = (0, 4, 0, 0, 0, 0) となります。

(a,b,c)=(3,3,2)(a, b, c) = (3, 3, 2) として操作を行うと (A1,A2,A3,A4,A5,A6)=(0,4,2,2,2,0)(A_1, A_2, A_3, A_4, A_5, A_6) = (0, 4, 2, 2, 2, 0) となります。

(a,b,c)=(1,1,1)(a, b, c) = (1, 1, 1) として操作を行うと (A1,A2,A3,A4,A5,A6)=(1,4,2,2,2,0)(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 2, 2, 0) となります。

(a,b,c)=(4,1,1)(a, b, c) = (4, 1, 1) として操作を行うと (A1,A2,A3,A4,A5,A6)=(1,4,2,1,2,0)(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 1, 2, 0) となります。

(a,b,c)=(6,1,5)(a, b, c) = (6, 1, 5) として操作を行うと (A1,A2,A3,A4,A5,A6)=(1,4,2,1,2,5)(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 1, 2, 5) となります。

このとき、コストの合計は 5+5+2+2+6=205 + 5 + 2 + 2 + 6 = 20 となります。


入力例 2Copy

Copy
5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

出力例 2Copy

Copy
5000000005

入力例 3Copy

Copy
8
2 3 3 1 2 1 3 1
3 4 1 2 5 3 1 2

出力例 3Copy

Copy
23

Score : 550550 points

Problem Statement

There is a circular cake that has been cut into NN equal slices by its radii.

Each piece is labeled with an integer from 11 to NN in clockwise order, and for each integer ii with 1iN1 \leq i \leq N, the piece ii is also referred to as piece N+iN + i.

Initially, every piece’s color is color 00.

You can perform the following operation any number of times:

  • Choose integers aa, bb, and cc such that 1a,b,cN1 \leq a, b, c \leq N. For each integer ii with 0i<b0 \leq i < b, change the color of piece a+ia + i to color cc. The cost of this operation is b+Xcb + X_c.

You want each piece ii (for 1iN1 \leq i \leq N) to have color CiC_i. Find the minimum total cost of operations needed to achieve this.

Constraints

  • 1N4001 \leq N \leq 400
  • 1CiN1 \leq C_i \leq N
  • 1Xi1091 \leq X_i \leq 10^9
  • All input values are integers.

Input

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

NN
C1C_1 C2C_2 \ldots CNC_N
X1X_1 X2X_2 \ldots XNX_N

Output

Print the answer.


Sample Input 1Copy

Copy
6
1 4 2 1 2 5
1 2 3 4 5 6

Sample Output 1Copy

Copy
20

Let AiA_i denote the color of piece ii. Initially, (A1,A2,A3,A4,A5,A6)=(0,0,0,0,0,0)(A_1, A_2, A_3, A_4, A_5, A_6) = (0, 0, 0, 0, 0, 0).

Performing an operation with (a,b,c)=(2,1,4)(a, b, c) = (2, 1, 4) changes (A1,A2,A3,A4,A5,A6)(A_1, A_2, A_3, A_4, A_5, A_6) to (0,4,0,0,0,0)(0, 4, 0, 0, 0, 0).

Performing an operation with (a,b,c)=(3,3,2)(a, b, c) = (3, 3, 2) changes (A1,A2,A3,A4,A5,A6)(A_1, A_2, A_3, A_4, A_5, A_6) to (0,4,2,2,2,0)(0, 4, 2, 2, 2, 0).

Performing an operation with (a,b,c)=(1,1,1)(a, b, c) = (1, 1, 1) changes (A1,A2,A3,A4,A5,A6)(A_1, A_2, A_3, A_4, A_5, A_6) to (1,4,2,2,2,0)(1, 4, 2, 2, 2, 0).

Performing an operation with (a,b,c)=(4,1,1)(a, b, c) = (4, 1, 1) changes (A1,A2,A3,A4,A5,A6)(A_1, A_2, A_3, A_4, A_5, A_6) to (1,4,2,1,2,0)(1, 4, 2, 1, 2, 0).

Performing an operation with (a,b,c)=(6,1,5)(a, b, c) = (6, 1, 5) changes (A1,A2,A3,A4,A5,A6)(A_1, A_2, A_3, A_4, A_5, A_6) to (1,4,2,1,2,5)(1, 4, 2, 1, 2, 5).

In this case, the total cost is 5+5+2+2+6=205 + 5 + 2 + 2 + 6 = 20.


Sample Input 2Copy

Copy
5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2Copy

Copy
5000000005

Sample Input 3Copy

Copy
8
2 3 3 1 2 1 3 1
3 4 1 2 5 3 1 2

Sample Output 3Copy

Copy
23


2025-04-10 (Thu)
23:39:24 +00:00