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
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define VAR(v,w) __typeof(w) v=(w)
#define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)
#define PB push_back
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) ((int)(c).size())
#define INT(x) int x; scanf("%d", &x)
#define STR(n,x) char x[n]; scanf("%s", x)

typedef vector<bool> VB;

string read() {
	INT(n);
	STR(2, c);
	string s;
	REP(i,n) {
		INT(a);
		s += string(a, c[0]);
		c[0] ^= 1;
	}
	return s;
}

bool check(const string& s, const string& t) {
	int n = SIZE(s), m = SIZE(t);
	VB p(n);
	p.resize(n + m, 1);
	do {
		string r;
		int i = 0, j = 0;
		FORE(it,p)
			if (*it) r.PB(t[j++]);
			else r.PB(s[i++]);
		int x = 0;
		FORE(it,r) {
			if (*it == '(') ++x;
			else --x;
			if (x < 0) break;
		}
		if (!x) return 1;
	} while (next_permutation(ALL(p)));
	return 0;
}

int main() {
	string s = read(), t = read();
	int n = SIZE(t);
	int r = 0;
	REP(i,n) FOR(j,i+1,n+1) r += check(s, t.substr(i, j - i));
	printf("%d\n", r);
}