1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define LL long long
#define int LL
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define VI vector<int>
#define PII pair<int,int>
#define LD long double

struct EncodedWord {
    vector<PII> runs;
    int total = 0;
    int length = 0;
};

EncodedWord read_encoded_word() {
    int blocks = 0;
    char first = 0;
    cin >> blocks >> first;

    EncodedWord word;
    char current = first;
    REP(i, blocks) {
        int repetitions = 0;
        cin >> repetitions;
        word.runs.PB({current, repetitions});
        word.total += (current == '(' ? repetitions : -repetitions);
        word.length += repetitions;
        current = (current == '(' ? ')' : '(');
    }
    return word;
}

string expand_word(const EncodedWord& word) {
    string out;
    out.reserve(SZ(word.runs) ? (size_t)word.length : 0);
    for (auto [c, len] : word.runs) {
        out.append((size_t)len, c);
    }
    return out;
}

int bracket_value(char c) {
    return c == '(' ? 1 : -1;
}

pair<VI, vector<char>> build_prefix_info(const string& s) {
    VI pref(SZ(s) + 1, 0);
    vector<char> reachable_empty(SZ(s) + 1, false);
    reachable_empty[0] = true;
    FOR(i, 1, SZ(s)) {
        pref[i] = pref[i - 1] + bracket_value(s[i - 1]);
        reachable_empty[i] = reachable_empty[i - 1] && (pref[i] >= 0);
    }
    return {pref, reachable_empty};
}

int solve_unrolled(const EncodedWord& s_enc, const EncodedWord& t_enc) {
    const string s = expand_word(s_enc);
    const string t = expand_word(t_enc);
    auto [pref_s, reachable_empty] = build_prefix_info(s);
    vector<char> prev(SZ(s) + 1, false), cur(SZ(s) + 1, false);

    int answer = 0;
    REP(left, SZ(t)) {
        prev = reachable_empty;
        int fragment_balance = 0;
        FOR(right, left, SZ(t) - 1) {
            fragment_balance += bracket_value(t[right]);

            cur[0] = prev[0] && (fragment_balance >= 0);
            FOR(i, 1, SZ(s)) {
                cur[i] = (pref_s[i] + fragment_balance >= 0) && (prev[i] || cur[i - 1]);
            }

            swap(prev, cur);
            if (pref_s[SZ(s)] + fragment_balance == 0 && prev[SZ(s)]) {
                ++answer;
            }
        }
    }

    return answer;
}

bool use_unrolled_solver(const EncodedWord& s, const EncodedWord& t) {
    if (s.length + t.length > 3000) return false;
    return true;
}

int single_need(const vector<PII>& runs, int idx, VI& memo) {
    if (idx >= SZ(runs)) {
        return 0;
    }
    if (memo[idx] != -1) {
        return memo[idx];
    }
    if (runs[idx].st == '(') {
        memo[idx] = max(0LL, single_need(runs, idx + 1, memo) - runs[idx].nd);
        return memo[idx];
    }
    return memo[idx] = runs[idx].nd + single_need(runs, idx + 1, memo);
}

int pair_need(const vector<PII>& a, const vector<PII>& b, int ia, int ib,
              vector<VI>& memo, VI& memo_a, VI& memo_b) {
    if (ia >= SZ(a)) {
        return single_need(b, ib, memo_b);
    }
    if (ib >= SZ(b)) {
        return single_need(a, ia, memo_a);
    }
    int& ans = memo[ia][ib];
    if (ans != -1) {
        return ans;
    }

    if (a[ia].st == '(' || b[ib].st == '(') {
        int gain = 0;
        int na = ia, nb = ib;
        if (a[ia].st == '(') {
            gain += a[na++].nd;
        }
        if (b[ib].st == '(') {
            gain += b[nb++].nd;
        }
        return ans = max(0LL, pair_need(a, b, na, nb, memo, memo_a, memo_b) - gain);
    }

    return ans = min(a[ia].nd + pair_need(a, b, ia + 1, ib, memo, memo_a, memo_b),
                     b[ib].nd + pair_need(a, b, ia, ib + 1, memo, memo_a, memo_b));
}

int need_merge(const vector<PII>& a, const vector<PII>& b) {
    vector<VI> memo(SZ(a), VI(SZ(b), -1));
    VI memo_a(SZ(a), -1), memo_b(SZ(b), -1);
    return pair_need(a, b, 0, 0, memo, memo_a, memo_b);
}

vector<PII> build_sub_runs(const vector<PII>& runs, int l, int a, int r, int b) {
    vector<PII> out;
    if (l == r) {
        out.PB({runs[l].st, b});
        return out;
    }
    out.PB({runs[l].st, a});
    FOR(i, l + 1, r - 1) {
        out.PB(runs[i]);
    }
    out.PB({runs[r].st, b});
    return out;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    EncodedWord s = read_encoded_word();
    EncodedWord t = read_encoded_word();
    if (use_unrolled_solver(s, t)) {
        cout << solve_unrolled(s, t) << '\n';
        return 0;
    }

    const int target = -s.total;
    const vector<PII>& rt = t.runs;
    vector<int> pref(SZ(rt) + 1, 0);
    REP(i, SZ(rt)) {
        pref[i + 1] = pref[i] + (rt[i].st == '(' ? rt[i].nd : -rt[i].nd);
    }

    int answer = 0;

    REP(i, SZ(rt)) {
        int sign = (rt[i].st == '(' ? 1 : -1);
        int len = sign * target;
        if (1 <= len && len <= rt[i].nd) {
            vector<PII> sub = {{rt[i].st, len}};
            if (need_merge(s.runs, sub) == 0) {
                answer += rt[i].nd - len + 1;
            }
        }
    }

    REP(l, SZ(rt)) {
        FOR(r, l + 1, SZ(rt) - 1) {
            int middle_sum = pref[r] - pref[l + 1];
            int sl = (rt[l].st == '(' ? 1 : -1);
            int sr = (rt[r].st == '(' ? 1 : -1);
            int c = (target - middle_sum) / sr;
            int k = -sl / sr;

            int lo = 1;
            int hi = rt[l].nd;
            if (k == 1) {
                lo = max(lo, 1 - c);
                hi = min(hi, rt[r].nd - c);
            } else {
                lo = max(lo, c - rt[r].nd);
                hi = min(hi, c - 1);
            }
            if (lo > hi) {
                continue;
            }

            map<int, int> cache;
            auto eval = [&](int a) -> int {
                auto it = cache.find(a);
                if (it != cache.end()) {
                    return it->nd;
                }
                int b = c + k * a;
                vector<PII> sub = build_sub_runs(rt, l, a, r, b);
                int val = need_merge(s.runs, sub);
                cache[a] = val;
                return val;
            };

            int L = lo;
            int R = hi;
            while (L < R) {
                int mid = (L + R) / 2;
                if (eval(mid) <= eval(mid + 1)) {
                    R = mid;
                } else {
                    L = mid + 1;
                }
            }
            int best = L;
            if (eval(best) != 0) {
                continue;
            }

            int left = lo;
            int right = best;
            while (left < right) {
                int mid = (left + right) / 2;
                if (eval(mid) == 0) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }
            int first = left;

            left = best;
            right = hi;
            while (left < right) {
                int mid = (left + right + 1) / 2;
                if (eval(mid) == 0) {
                    left = mid;
                } else {
                    right = mid - 1;
                }
            }
            int last = left;

            answer += last - first + 1;
        }
    }

    cout << answer << '\n';
    return 0;
}