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
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define cr(v, n) (v).clear(), (v).resize(n);
using namespace std;
using lint = long long;
using pi = array<lint, 2>;

ostream &operator<<(ostream &os, const __int128_t &v) {
	if (!v) {
		return (os << "0");
	}
	__int128_t n = v;
	if (v < 0) {
		os << '-';
		n = -n;
	}
	string s;
	for (; n; n /= 10) {
		s += (n % 10) + '0';
	}
	ranges::reverse(s);
	return (os << s);
}

bool solve(vector<lint> x, vector<lint> y) {
	vector<int> sx = {0}, sy = {0};
	for (int i = 1; i < x.size(); i++) {
		if (x[sx.back()] <= x[i])
			sx.push_back(i);
	}
	for (int i = 1; i < y.size(); i++) {
		if (y[sy.back()] >= y[i])
			sy.push_back(i);
	}
	vector<lint> prec;
	for (int i = 0; i + 1 < sy.size(); i++) {
		prec.push_back(*max_element(y.begin() + sy[i] + 1, y.begin() + sy[i + 1] + 1));
	}
	int cs = 0, ce = 0;
	for (int i = 0; i < sx.size(); i++) {
		while (ce + 1 < sy.size()) {
			lint nxtmax = prec[ce];
			if (nxtmax <= x[sx[i]])
				ce++;
			else
				break;
		}
		if (i + 1 < sx.size()) {
			lint nxtmin = *min_element(x.begin() + sx[i] + 1, x.begin() + sx[i + 1] + 1);
			while (cs <= ce && y[sy[cs]] > nxtmin)
				cs++;
			if (cs > ce)
				return 0;
		}
	}
	return ce + 1 == sy.size();
}

int mx;
vector<lint> u1, u2;
vector<lint> w;

lint dolve(vector<lint> w) {
	int m = min_element(all(w)) - w.begin();
	vector<lint> w1(w.begin(), w.begin() + m + 1);
	vector<lint> w2(w.begin() + m, w.end());
	reverse(all(w2));
	return solve(u1, w1) && solve(u2, w2);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	vector<lint> v = {0};
	{
		int n;
		cin >> n;
		string b;
		cin >> b;
		int mode = (b[0] == '(' ? 1 : -1);
		lint cur = 0;
		for (int j = 0; j < n; j++) {
			int x;
			cin >> x;
			cur += mode * x;
			v.push_back(cur);
			mode *= -1;
		}
	}
	{
		w.push_back(0);
		int n;
		cin >> n;
		string b;
		cin >> b;
		int mode = (b[0] == '(' ? -1 : +1);
		lint cur = 0;
		for (int j = 0; j < n; j++) {
			int x;
			cin >> x;
			cur += mode * x;
			w.push_back(cur);
			mode *= -1;
		}
	}
	int mx = max_element(all(v)) - v.begin();
	u1 = vector<lint>(v.begin(), v.begin() + mx + 1);
	u2 = vector<lint>(v.begin() + mx, v.end());
	reverse(all(u2));
	auto norm = [&](vector<lint> v) {
		lint curmin = v[0], curmax = v[0];
		int last_upd = 1;
		vector<lint> newval = {v[0]};
		for (int j = 1; j < sz(v); j++) {
			if (curmin > v[j]) {
				curmin = v[j];
				if (last_upd == 1) {
					newval.push_back(curmin);
				} else {
					newval.back() = curmin;
				}
				last_upd = 0;
			} else if (curmax < v[j]) {
				curmax = v[j];
				if (last_upd == 0) {
					newval.push_back(curmax);
				} else
					newval.back() = curmax;
				last_upd = 1;
			}
		}
		if (newval.back() != curmax)
			newval.push_back(curmax);
		return newval;
	};
	u1 = norm(u1);
	u2 = norm(u2);
	lint ans = 0;
	lint sum = v.back();
	for (int i = 0; i + 1 < sz(w); i++) {
		if (w[i] < w[i + 1]) {
			for (lint p = w[i]; p < w[i + 1]; p++) {
				vector<lint> cur = {0};
				for (int q = i + 1; q < sz(w); q++) {
					lint A = cur.back();
					lint B = w[q] - p;
					if (A > sum && sum >= B) {
						cur.push_back(sum);
						if (dolve(cur))
							ans++;
						cur.pop_back();
					}
					if (A < sum && sum <= B) {
						cur.push_back(sum);
						if (dolve(cur))
							ans++;
						cur.pop_back();
					}
					cur.push_back(B);
				}
			}
		} else if (w[i] > w[i + 1]) {
			for (lint p = w[i]; p > w[i + 1]; p--) {
				vector<lint> cur = {0};
				for (int q = i + 1; q < sz(w); q++) {
					lint A = cur.back();
					lint B = w[q] - p;
					if (A > sum && sum >= B) {
						cur.push_back(sum);
						if (dolve(cur))
							ans++;
						cur.pop_back();
					}
					if (A < sum && sum <= B) {
						cur.push_back(sum);
						if (dolve(cur))
							ans++;
						cur.pop_back();
					}
					cur.push_back(B);
				}
			}
		}
	}
	cout << ans << "\n";
}