#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); for (ll& x : a) cin >> x; vector> dp = vector>(2 * n + 1, vector(n + 1, linf)); dp[0][0] = 0; rep(i, 2 * n) { rep(j, n + 1) { if (dp[i][j] == linf) continue; if (j + 1 < n) { ll cost = (s[i] == '(' ? 0 : a[i]); dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + cost); } if (j > 0) { ll cost = (s[i] == ')' ? 0 : a[i]); dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j] + cost); } } } cout << dp[n * 2][0] << endl; return 0; }