#ifdef DEBUG #define _GLIBCXX_DEBUG #else #pragma GCC target("avx2") #pragma GCC optimize("Ofast,unroll-loops") #endif #include #include #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define ALL(a) a.begin(), a.end() #undef long #define long long long #define ll long using namespace std; using mint = atcoder::modint998244353; ostream& operator<<(ostream& os, mint& a) { return os << a.val(); } template ostream& operator<<(ostream& os, vector& a) { const int n = a.size(); rep(i, n) os << a[i] << " \n"[i + 1 == n]; return os; } template ostream& operator<<(ostream& os, array& a) { rep(i, n) os << a[i] << " \n"[i + 1 == n]; return os; } template istream& operator>>(istream& is, vector& a) { for (T& i : a) is >> i; return is; } template bool chmin(T& x, T y) { if (x > y) { x = y; return true; } return false; } template bool chmax(T& x, T y) { if (x < y) { x = y; return true; } return false; } void solve() { int n; string s; cin >> n >> s; const int nn = 2 * n; vector a(nn); cin >> a; constexpr long inf = 1e18; vector> d(n + 1, vector(n + 1, inf)); d[0][0] = 0; rep(i, n + 1) rep(j, n + 1) { if (i < j) continue; const int ij = i + j; if (ij == nn) continue; if (s[ij] == '(') { if (i != n) chmin(d[i + 1][j], d[i][j]); if (j != n) chmin(d[i][j + 1], d[i][j] + a[ij]); } else { if (j != n) chmin(d[i][j + 1], d[i][j]); if (i != n) chmin(d[i + 1][j], d[i][j] + a[ij]); } } cout << d[n][n] << endl; } int main() { // srand((unsigned)time(NULL)); cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(40); solve(); return 0; }