#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string S; cin >> N >> S; vector dp(N + 1, 1LL << 60); dp[0] = 0; for (int i = 0; i < 2 * N; i++) { vector ep(N + 1, 1LL << 60); int A; cin >> A; for (int j = 0; j <= N; j++) { if (j > 0) { ep[j - 1] = min(ep[j - 1], dp[j] + (S[i] == '(' ? A : 0)); } if (j < N) { ep[j + 1] = min(ep[j + 1], dp[j] + (S[i] == '(' ? 0 : A)); } } dp = ep; } cout << dp[0] << endl; }