#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") // par: must represent a forest. // The roots should point to -1 or n. // T: monoid // T() should return the identity. // T::append(const T &t) should multiply t from right. // T::operator<(const T &t) should represent the order to optimize. // Small to large. // It must be a weak order (for the possible products of ts). template struct ForestOptimalOrder { int n; bool isForest; T total; vector us; ForestOptimalOrder() : n(0), isForest(false), total() {} ForestOptimalOrder(const vector &par, vector ts) : n(par.size()), isForest(false), total() { assert(n == static_cast(ts.size())); uf.assign(n + 1, -1); std::priority_queue que; vector tails(n + 1, n), nxt(n + 1, -1); for (int u = 0; u < n; ++u) que.push(Entry{ts[u], u, tails[u] = u}); for (; !que.empty(); ) { const int v = que.top().head, tail = que.top().tail; que.pop(); if (tails[v] == tail) { // v as early as possible ==> right after its parent const int u = root((~par[v]) ? par[v] : n); if (!connect(u, v)) return; nxt[tails[u]] = v; tails[u] = tail; if (u == n) { total.append(ts[v]); } else { ts[u].append(ts[v]); que.push(Entry{ts[u], u, tails[u]}); } } } isForest = true; us.resize(n); for (int j = 0, u = n; j < n; ++j) us[j] = u = nxt[u]; } struct Entry { T t; int head, tail; // reversed order bool operator<(const Entry &e) const { return (e.t < t); } }; vector uf; int root(int u) { return (uf[u] < 0) ? u : (uf[u] = root(uf[u])); } // root(u) becomes the parent bool connect(int u, int v) { u = root(u); v = root(v); if (u == v) return false; uf[u] += uf[v]; uf[v] = u; return true; } }; //////////////////////////////////////////////////////////////////////////////// struct Info { // (weight, score) -> (weight + a, score + b * weight + c) Int a, b, c; Info() : a(0), b(0), c(0) {} void append(const Info &t) { b += t.b; c += t.b * a + t.c; a += t.a; } bool operator<(const Info &t) const { return (t.b * a < b * t.a); } }; int N; char S[200'010]; vector A; vector us, to; vector par; void parse(int l, int r, int p) { for (int x = l; x < r; x = to[x] + 1) { const int u = us[x]; par[u] = p; parse(x + 1, to[x], u); } } int main() { for (; ~scanf("%d", &N); ) { scanf("%s", S); A.resize(N); for (int u = 0; u < N; ++u) { scanf("%lld", &A[u]); } us.assign(2*N, -1); to.assign(2*N, -1); { vector stack; int u = 0; for (int x = 0; x < 2*N; ++x) { if (S[x] == '(') { stack.push_back(x); us[x] = u++; } else { const int y = stack.back(); stack.pop_back(); to[y] = x; to[x] = y; } } } par.assign(N, -2); parse(0, 2*N, -1); cerr<<"par = "< ts(N); for (int u = 0; u < N; ++u) { ts[u].a = 1; ts[u].b = A[u]; ts[u].c = 0; } const ForestOptimalOrder foo(par, ts); cerr<<"foo.us = "<