n=int(input()) S=str(input()) A=list(map(int,input().split())) dp=[[10**18 for i in range(n*2+1)] for j in range(2*n+1)] dp[0][0]=0 for i in range(n*2): for j in range(n*2): if dp[i][j]!=10**18: if S[i]=='(': dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]) if j!=0: dp[i+1][j-1]=min(dp[i+1][j-1],dp[i][j]+A[i]) else: if j!=0: dp[i+1][j-1]=min(dp[i+1][j+1],dp[i][j]) dp[i+1][j+1]=min(dp[i+1][j-1],dp[i][j]+A[i]) print(dp[n*2][0])