n = int(input()) s = input() a = list(map(int, input().split())) INF = 10 ** 18 dp = [[INF] * (2 * n + 1) for _ in range(2 * n + 1)] dp[0][0] = 0 for i in range(2 * n): for j in range(2 * n + 1): if dp[i][j] == INF: continue if s[i] == '(': dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]) if j: dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j] + a[i]) else: dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + a[i]) if j: dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j]) print(dp[2 * n][0])