using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3");
WillReturn.Add("))(");
WillReturn.Add("3 5 7");
WillReturn.Add("2 6 5");
//8
}
else if (InputPattern == "Input2") {
WillReturn.Add("1");
WillReturn.Add("(");
WillReturn.Add("10");
WillReturn.Add("20");
//20
}
else if (InputPattern == "Input3") {
WillReturn.Add("10");
WillReturn.Add("))())((()(");
WillReturn.Add("13 18 17 3 20 20 6 14 14 2");
WillReturn.Add("20 1 19 5 2 19 2 19 9 4");
//18
}
else if (InputPattern == "Input4") {
WillReturn.Add("4");
WillReturn.Add("()()");
WillReturn.Add("17 8 3 19");
WillReturn.Add("5 3 16 3");
//0
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
string S = InputList[1];
long[] CArr = InputList[2].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long[] DArr = InputList[3].Split(' ').Select(pX => long.Parse(pX)).ToArray();
// 開き括弧をS、閉じ括弧をEに変換
S = S.Replace('(', 'S').Replace(')', 'E');
// 最小コスト[開き括弧の数]なDP表
var PrevDP = new Dictionary<long, long>();
PrevDP[0] = 0;
for (int I = 0; I <= S.Length - 1; I++) {
var CurrDP = new Dictionary<long, long>();
foreach (var EachPair in PrevDP) {
Action<long, long> UpsertAct = (pNewKey, pNewVal) =>
{
// マイナス(開き括弧が無い状態での閉じ括弧)は不可
if (pNewKey < 0) return;
if (CurrDP.ContainsKey(pNewKey)) {
if (CurrDP[pNewKey] <= pNewVal) {
return;
}
}
CurrDP[pNewKey] = pNewVal;
};
// そのまま使う場合
if (S[I] == 'S') {
UpsertAct(EachPair.Key + 1, EachPair.Value);
}
else {
UpsertAct(EachPair.Key - 1, EachPair.Value);
}
// 変換する場合
if (S[I] == 'S') {
UpsertAct(EachPair.Key - 1, EachPair.Value + CArr[I]);
}
else {
UpsertAct(EachPair.Key + 1, EachPair.Value + CArr[I]);
}
// 消去する場合
UpsertAct(EachPair.Key, EachPair.Value + DArr[I]);
}
PrevDP = CurrDP;
}
Console.WriteLine(PrevDP[0]);
}
}