import heapq N = int(input()) S = list(input()) A = list(map(int, input().split())) ans = 0 h = [] cnt = 0 for s,a in zip(S,A): if s=='(': cnt+=1 else: heapq.heappush(h,a) cnt-=1 if cnt<0: cnt+=2 ans+=heapq.heappop(h) h = [] cnt = 0 for s,a in zip(S[::-1],A[::-1]): if s==')': cnt+=1 else: heapq.heappush(h,a) cnt-=1 if cnt<0: cnt+=2 ans+=heapq.heappop(h) print(ans)