# coding: utf-8 import heapq def isValid(S): a=0 for c in S: if c=='(': a+=1 elif c==')': a-=1 else: raise Exception if a<0: return False return a==0 def change(S,i): L=list(S) if S[i]=='(': L[i]=')' elif S[i]==')': L[i]='(' else: raise Exception return "".join(L) def dijkstra(n,S,A): closed=set() temp={} heap=[] heapq.heappush(heap,(0,S)) while heap: tupl=heapq.heappop(heap) T=tupl[1] cost=tupl[0] if T not in closed: closed.add(T) if(isValid(T)): return cost for i in range(len(T)): U=change(T,i) if U not in closed: if U not in temp: temp[U]=cost+A[i] else: temp[U]=min(temp[U],cost+A[i]) heapq.heappush(heap,(temp[U],U)) raise Exception if __name__ == "__main__": print(dijkstra(int(input()),input(),list(map(int,input().split()))))