#abc346d. D - Gomamayo Sequence

D - Gomamayo Sequence

Score: 400400 points

问题陈述

给定一个由01组成的字符串SS,长度为NN

一个由01组成的字符串TT好字符串,当且仅当它满足以下条件:

  • 存在一个且只有一个整数ii,使得1iN11 \leq i \leq N - 1,并且TT的第ii个和第(i+1)(i + 1)个字符相同。

对于每个i=1,2,,Ni = 1,2,\ldots, N,你可以选择是否执行以下操作一次:

  • 如果SS的第ii个字符是0,则将其替换为1,反之亦然。如果执行了这个操作,其成本为CiC_i

找出使SS成为好字符串所需的最小总成本。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a string SS of length NN consisting of 0 and 1.

A string TT of length NN consisting of 0 and 1 is a good string if and only if it satisfies the following condition:

  • There is exactly one integer ii such that 1iN11 \leq i \leq N - 1 and the ii-th and (i+1)(i + 1)-th characters of TT are the same.

For each i=1,2,,Ni = 1,2,\ldots, N, you can choose whether or not to perform the following operation once:

  • If the ii-th character of SS is 0, replace it with 1, and vice versa. The cost of this operation, if performed, is CiC_i.

Find the minimum total cost required to make SS a good string.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • SS is a string of length NN consisting of 0 and 1.
  • 1Ci1091 \leq C_i \leq 10^9
  • NN and CiC_i are integers.

Input

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

NN

SS

C1C_1 C2C_2 \ldots CNC_N

Output

Print the answer.

Sample Input 1

5
00011
3 9 2 6 4

Sample Output 1

7

Performing the operation for i=1,5i = 1, 5 and not performing it for i=2,3,4i = 2, 3, 4 makes S=S = 10010, which is a good string. The cost incurred in this case is 77, and it is impossible to make SS a good string for less than 77, so print 77.

Sample Input 2

4
1001
1 2 3 4

Sample Output 2

0

Sample Input 3

11
11111100111
512298012 821282085 543342199 868532399 690830957 973970164 928915367 954764623 923012648 540375785 925723427

Sample Output 3

2286846953

update @ 2024/5/16 17:17:59