#include using ll = long long; #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define rreps(i, n) for (int i = ((int)(n)); i > 0; --i) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) #define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++) constexpr int inf = 2000'000'000; constexpr ll linf = 4000000000000000000ll; constexpr ll M7 = 1000000007ll; constexpr ll M09 = 1000000009ll; constexpr ll M9 = 998244353ll; #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) using namespace std; template inline ostream& operator<<(ostream& os, const vector& v) { for (auto itr = v.begin(); itr != v.end(); ++itr) { os << *itr; if (itr != v.end() - 1) { os << " "; } } return os; } template std::ostream& operator<<(std::ostream& os, const std::pair& p) noexcept { return os << "(" << p.first << ", " << p.second << ")"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; string s; cin >> n >> s; vector a(2 * n); rep(i, 2 * n) cin >> a[i]; vector> dp = vector>(2 * n, vector(2 * n, linf)); for (int len = 2; len <= 2 * n; len += 2) { for (int l = 0; l < 2 * n; l++) { int r = l + len - 1; if (r >= 2 * n) break; if (len == 2) { ll x = 0; if (s[l] == ')') x += a[l]; if (s[r] == '(') x += a[r]; dp[l][r] = x; } else { for (int i = l + 1; i < r; i += 2) dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r]); ll x = 0; if (s[l] == ')') x += a[l]; if (s[r] == '(') x += a[r]; dp[l][r] = min(dp[l][r], x + dp[l + 1][r - 1]); } } } cout << dp[0][2 * n - 1] << endl; return 0; }