import sys
from itertools import permutations
while True:
try:
n = int(input())
s = input()
val = list(map(int,input().split()))
bod = [[0]*n for _ in range(3)]
for i in range(n):
if s[i] == '0':
bod[0][i] = bod[0][i-1]
bod[1][i] = bod[1][i-1] + val[i]
bod[2][i] = bod[2][i-1] + val[i]
elif s[i] == '1':
bod[0][i] = bod[0][i-1] + val[i]
bod[1][i] = bod[1][i-1]
bod[2][i] = bod[2][i-1] + val[i]
elif s[i] == '2':
bod[0][i] = bod[0][i-1] + val[i]
bod[1][i] = bod[1][i-1] + val[i]
bod[2][i] = bod[2][i-1]
ans = bod[0][-1]
for item in permutations([0,1,2],2):
aindex,bindex = item
for i in [0,1,2]:
if i not in item:
cindex = i
break
templ = [bod[aindex][0]-bod[bindex][0]]
for i in range(1,n):
temp = bod[aindex][i]-bod[bindex][i]
templ.append(min(templ[-1],temp))
for i in range(n):
out = templ[i] + bod[bindex][i]-bod[cindex][i]+bod[cindex][-1]
ans = min(ans,out)
for i in range(n):
out = -templ[i] + bod[aindex][i]-bod[cindex][i]+bod[cindex][-1]
ans = min(ans,out)
print(ans)
except:
break

京公网安备 11010502036488号