#include using namespace std; using namespace chrono; #if __has_include() #include using namespace atcoder; #endif int main() { int64_t n; string s; cin >> n >> s; vector as(2 * n); for (auto &&a : as) { cin >> a; } int64_t inf = (1LL << 60); vector dp(2 * n + 1, vector(2 * n + 1, inf)); dp[0][0] = 0; for (int64_t i = 0; i < 2 * n; i++) { for (int64_t j = 0; j <= 2 * n; j++) { if (s[i] == '(') { if (j + 1 <= 2 * n) { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]); } if (0 <= j - 1) { dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j] + as[i]); } } else if (s[i] == ')') { if (j + 1 <= 2 * n) { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + as[i]); } if (0 <= j - 1) { dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j]); } } } } cout << dp[2 * n][0] << endl; return 0; }