stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, m, n) for (int i = m; i < n; ++i)



bool solve() {

    string S;

    int x, y;

    cin >> S >> x >> y;

    vector<vector<int>> C(2);

    int cnt = 0;

    rep(l, 0, S.size()) {

        int r = l;

        while(r < S.size() && S[r] == 'F') r++;

        C[(cnt++) % 2].push_back(r - l);

        l = r;

    }

    int N = C[0].size();

    int M = C[1].size();

    vector<vector<int>> dp(N + 1, vector<int>(16001));

    vector<vector<int>> ep(M + 1, vector<int>(16001));

    int g = 8000;

    dp[1][C[0][0] + g] = 1;

    rep(i, 1, N) rep(j, -8000, 8001) {

        if(dp[i][j + g] == 0) continue;

        dp[i + 1][j + C[0][i] + g] = 1;

        dp[i + 1][j - C[0][i] + g] = 1;

    }

    ep[0][g] = 1;

    rep(i, 0, M) rep(j, -8000, 8001) {

        if(ep[i][j + g] == 0) continue;

        ep[i + 1][j + C[1][i] + g] = 1;

        ep[i + 1][j - C[1][i] + g] = 1;

    }

    return dp[N][x + g] && ep[M][y + g];

}



int main() {

    cout << (solve() ? "Yes" : "No") << '\n';

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.5994333158622247x):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, m, n) for (int i = m; i < n; ++i)



bool solve() {

    string S;

    int x, y;

    cin >> S >> x >> y;

    vector<vector<int>> C(2);

    int cnt = 0;

    rep(l, 0, S.size()) {

        int r = l;

        while(r < S.size() && S[r] == 'F') r++;

        C[(cnt++) % 2].push_back(r - l);

        l = r;

    }

    int N = C[0].size();

    int M = C[1].size();

    vector<vector<bool>> dp(N + 1, vector<bool>(16001));

    vector<vector<bool>> ep(M + 1, vector<bool>(16001));

    int g = 8000;

    dp[1][C[0][0] + g] = 1;

    rep(i, 1, N) rep(j, -8000, 8001) {

        if(dp[i][j + g] == 0) continue;

        dp[i + 1][j + C[0][i] + g] = 1;

        dp[i + 1][j - C[0][i] + g] = 1;

    }

    ep[0][g] = 1;

    rep(i, 0, M) rep(j, -8000, 8001) {

        if(ep[i][j + g] == 0) continue;

        ep[i + 1][j + C[1][i] + g] = 1;

        ep[i + 1][j - C[1][i] + g] = 1;

    }

    return dp[N][x + g] && ep[M][y + g];

}



int main() {

    cout << (solve() ? "Yes" : "No") << '\n';

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <stdio.h>



#define N 16'001

#define O 8'000



using namespace std;



bool X[2][N]{};

bool Y[2][N]{};



int main() {

  string s;

  int x, y;

  cin >> s;

  cin >> x >> y;



  int i = 0;

  int M = s.size();

  int d = 0;

  while (i < M && s[i++] == 'F') {

    ++d;

  }

  X[0][d + O] = true;

  d = 0;

  while (i < M && s[i++] == 'F') {

    ++d;

  }

  Y[0][d + O] = true;

  Y[0][-d + O] = true;



  int j = 1;

  while (i < M) {

    d = 0;

    while (i < M && s[i++] == 'F') {

      ++d;

    }

    for (int k = 0; k < N; ++k) {

      if ((k - d) >= 0 && (k + d) < N) {

        X[j % 2][k] = X[(j - 1) % 2][k - d] || X[(j - 1) % 2][k + d];

      } else if ((k - d) >= 0) {

        X[j % 2][k] = X[(j - 1) % 2][k - d];

      } else if ((k + d) < N) {

        X[j % 2][k] = X[(j - 1) % 2][k + d];

      }

    }



    d = 0;

    while (i < M && s[i++] == 'F') {

      ++d;

    }

    for (int k = 0; k < N; ++k) {

      if ((k - d) >= 0 && (k + d) < N) {

        Y[j % 2][k] = Y[(j - 1) % 2][k - d] || Y[(j - 1) % 2][k + d];

      } else if ((k - d) >= 0) {

        Y[j % 2][k] = Y[(j - 1) % 2][k - d];

      } else if ((k + d) < N) {

        Y[j % 2][k] = Y[(j - 1) % 2][k + d];

      }

    }

    ++j;

  }

  bool res = X[(j - 1) % 2][x + O] && Y[(j - 1) % 2][y + O];

  if (res) {

    printf("Yes\n");

  } else {

    printf("No\n");

  }

}

// Faster C++ solution for the same problem (measured speedup: 1.1059494608651386x):
#include <iostream>

#include <stdio.h>



#define N 16'001

#define O 8'000



using namespace std;



bool X[2][N]{};

bool Y[2][N]{};



int main() {

  string s;

  int x, y;

  cin >> s;

  cin >> x >> y;



  int i = 0;

  int M = s.size();

  int d = 0;

  while (i < M && s[i++] == 'F') {

    ++d;

  }

  X[0][d + O] = true;

  d = 0;

  while (i < M && s[i++] == 'F') {

    ++d;

  }

  Y[0][d + O] = true;

  Y[0][-d + O] = true;



  int j = 1;

  while (i < M) {

    d = 0;

    while (i < M && s[i++] == 'F') {

      ++d;

    }

    for (int k = 0; k <= 2 * M; ++k) {

      if ((k - d) >= 0 && (k + d) <= 2 * M) {

        X[j % 2][k - M + O] = X[(j - 1) % 2][k - M + O - d] || X[(j - 1) % 2][k - M + O + d];

      } else if ((k - d) >= 0) {

        X[j % 2][k - M + O] = X[(j - 1) % 2][k - M + O - d];

      } else if ((k + d) < N) {

        X[j % 2][k - M + O] = X[(j - 1) % 2][k - M + O + d];

      }

    }



    d = 0;

    while (i < M && s[i++] == 'F') {

      ++d;

    }

    for (int k = 0; k <= 2 * M; ++k) {

      if ((k - d) >= 0 && (k + d) < N) {

        Y[j % 2][k - M + O] = Y[(j - 1) % 2][k - M + O - d] || Y[(j - 1) % 2][k - M + O + d];

      } else if ((k - d) >= 0) {

        Y[j % 2][k - M + O] = Y[(j - 1) % 2][k - M + O - d];

      } else if ((k + d) < N) {

        Y[j % 2][k - M + O] = Y[(j - 1) % 2][k - M + O + d];

      }

    }

    ++j;

  }

  bool res = X[(j - 1) % 2][x + O] && Y[(j - 1) % 2][y + O];

  if (res) {

    printf("Yes\n");

  } else {

    printf("No\n");

  }

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



#define int long long



const int N = 16e3 + 10;

int x, y, _dp[2][2 * N + 100], *dp[2];

string s;

vector <int> vec[2];



void fill_dp(int indx) {

	for (int i = -N + 1; i < N; i++)

		dp[1][i] = 0;

	dp[1][0] = 1;

	

	for (int i = 0; i < vec[indx].size(); i++) {

		int now = (i & 1), prv = (now ^ 1), x = vec[indx][i];

		for (int j = -N + 1; j < N; j++) {

			dp[now][j] = 0;

			if (j - x > -N)

				dp[now][j] = dp[prv][j - x];

			if (j + x < N)

				dp[now][j] = max(dp[now][j], dp[prv][j + x]);

		}

	}

}



int32_t main() {

	ios::sync_with_stdio(0);

	cin.tie(0);

	

	dp[0] = _dp[0] + N + 50;

	dp[1] = _dp[1] + N + 50;

	

	cin >> s >> x >> y;

	while (s.size() && s.back() == 'T')

		s.pop_back();

	if (!s.size())

		return cout << (!x && !y? "Yes\n": "No\n"), 0;

	

	int tmp = 0, indx = 0;

	while (indx < s.size() && s[indx] == 'F')

		tmp++, indx++;

	

	int last = 0, num = 0;

	for (int i = indx; i < s.size(); i++) {

		if (s[i] == 'T')

			vec[num & 1].push_back(last), last = 0, num++;

		else

			last++;

	}

	vec[num & 1].push_back(last);

	

	fill_dp(0);

	if (!dp[((int)vec[0].size() & 1) ^ 1][x - tmp])

		return cout << "No\n", 0;

	fill_dp(1);

	cout << (dp[((int)vec[1].size() & 1) ^ 1][y]? "Yes\n": "No\n");

}

// Faster C++ solution for the same problem (measured speedup: 1.7129268993218787x):
#include <bits/stdc++.h>

using namespace std;

const int N = 8010;

int a[N], b[N];

char lx[N];



void fail() {

  puts("No");

  exit(0);

}



int f[N];

void gao(int a[], int tot, int flg) {

  int sum = 0;

  if(tot >= 0) {

    for(int i = 1; i <= a[0]; ++i) sum += a[i];

    sum -= tot;

  } else {

    for(int i = 1; i <= a[0]; ++i) {

      if(i == 1) {

        if(flg) sum += a[i];

        else sum -= a[i];

      } else {

        sum -= a[i];

      }

    }

    sum = tot - sum;

  }

  if(sum < 0 || (sum & 1)) fail();

  sum /= 2;

  memset(f, 0, sizeof f);

  f[0] = 1;

  for(int i = 1; i <= a[0]; ++i) {

    if(flg && i == 1) continue;

    for(int j = sum; j >= a[i]; --j) {

      if(f[j - a[i]]) f[j] = 1;

    }

  }

  if(!f[sum]) fail();

}



int main() {

  int x, y;

  scanf("%s%d%d", lx + 1, &x, &y);

  int len = strlen(lx + 1), res = 0;

  int now = 1;

  for(int i = 1; i <= len; ++i) {

    if(lx[i] == 'F') res++;

    else {

      if(now) a[++a[0]] = res;

      else b[++b[0]] = res;

      now ^= 1;

      res = 0;

    }

  }

  if(res) {

    if(now) a[++a[0]] = res;

    else b[++b[0]] = res;

  }

/*  for(int i = 1; i <= a[0]; ++i)

    printf("%d ", a[i]);

  puts("");

  for(int i = 1; i <= b[0]; ++i) 

    printf("%d ", b[i]);

  puts("");*/

  gao(a, x, 1), gao(b, y, 0);

  puts("Yes");

  return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

ll read(){

	ll a=0,b=getchar(),c=1;

	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();

	while(isdigit(b))a=a*10+b-'0',b=getchar();

	return a*c;

}

const ll mod=998244353;

ll n,ri,le,s0,f[1000005];

struct node{

	int x,y;

	bool operator<(node b)const{

		return x-y>b.x-b.y;

	}

}a[1000005];

char s[1000005];

int main(){

	n=read();

	for(int i=1;i<=n;i++){

		ll l=0,r=0;

		scanf("%s",s+1);

		for(int j=1;s[j];j++)

			if(s[j]=='(')l++;

			else{

				if(l)l--;

				else r++;

			}

		if(!l and r)ri+=r;

		else if(l and !r)le+=l;

		else a[i]=(node){l,r};	

		s0+=l-r;

	}

	if(s0!=0)return puts("No"),0;

	sort(a+1,a+1+n);

	for(int i=1;i<=n;i++){

		if(le<a[i].y)

			return puts("No"),0;

		le-=a[i].y-a[i].x;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.214373388675768x):
#include <cstdio>

#include <algorithm>

using namespace std;

struct node{

	int l,r;

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ri,le;

	int i,j;

	le=ri=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].r)

			return puts("No"),0;

		le-=a[i].r,le+=a[i].l;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef int ll;

ll read(){

	ll a=0,b=getchar(),c=1;

	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();

	while(isdigit(b))a=a*10+b-'0',b=getchar();

	return a*c;

}

const ll mod=998244353;

ll n,ri,le,s0,f[1000005];

struct node{

	int x,y;

	bool operator<(node b)const{

		return x-y>b.x-b.y;

	}

}a[1000005];

char s[1000005];

int main(){

	n=read();

	for(int i=1;i<=n;i++){

		ll l=0,r=0;

		scanf("%s",s+1);

		for(int j=1;s[j];j++)

			if(s[j]=='(')l++;

			else{

				if(l)l--;

				else r++;

			}

		if(!l and r)ri+=r;

		else if(l and !r)le+=l;

		else a[i]=(node){l,r};	

		s0+=l-r;

	}

	if(s0!=0)return puts("No"),0;

	sort(a+1,a+1+n);

	for(int i=1;i<=n;i++){

		if(le<a[i].y)

			return puts("No"),0;

		le-=a[i].y-a[i].x;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.215075327226263x):
#include <cstdio>

#include <algorithm>

using namespace std;

struct node{

	int l,r;

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ri,le;

	int i,j;

	le=ri=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].r)

			return puts("No"),0;

		le-=a[i].r,le+=a[i].l;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef int ll;

ll read(){

	ll a=0,b=getchar(),c=1;

	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();

	while(isdigit(b))a=a*10+b-'0',b=getchar();

	return a*c;

}

const ll mod=998244353;

ll n,ri,le,s0,f[1000005];

struct node{

	int x,y;

	/*bool operator<(node b)const{

		return x-y>b.x-b.y;

	}*/

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.x-a.y>b.x-b.y;

}

int main(){

	n=read();

	for(int i=1;i<=n;i++){

		ll l=0,r=0;

		scanf("%s",s+1);

		for(int j=1;s[j];j++)

			if(s[j]=='(')l++;

			else{

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

		//s0+=l-r;

	}

	//if(s0!=0)return puts("No"),0;

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].y)

			return puts("No"),0;

		le-=a[i].y-a[i].x;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.214439832058292x):
#include <cstdio>

#include <algorithm>

using namespace std;

struct node{

	int l,r;

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ri,le;

	int i,j;

	le=ri=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].r)

			return puts("No"),0;

		le-=a[i].r,le+=a[i].l;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef int ll;

ll read(){

	ll a=0,b=getchar(),c=1;

	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();

	while(isdigit(b))a=a*10+b-'0',b=getchar();

	return a*c;

}

const ll mod=998244353;

ll n,ri,le,s0,f[1000005];

struct node{

	int x,y;

	/*bool operator<(node b)const{

		return x-y>b.x-b.y;

	}*/

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.x-a.y>b.x-b.y;

}

int main(){

	//n=read();

	scanf("%d",&n);

	for(int i=1;i<=n;i++){

		ll l=0,r=0;

		scanf("%s",s+1);

		for(int j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

		//s0+=l-r;

	}

	//if(s0!=0)return puts("No"),0;

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].y)

			return puts("No"),0;

		le-=a[i].y-a[i].x;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.218091174888775x):
#include <cstdio>

#include <algorithm>

using namespace std;

struct node{

	int l,r;

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ri,le;

	int i,j;

	le=ri=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].r)

			return puts("No"),0;

		le-=a[i].r,le+=a[i].l;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,ri,le;

struct node{

	int x,y;

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.x-a.y>b.x-b.y;

}

int main(){

	//n=read();

	int i,j;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].y)

			return puts("No"),0;

		le-=a[i].y-a[i].x;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.216768210498947x):
#include <cstdio>

#include <algorithm>

using namespace std;

struct node{

	int l,r;

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ri,le;

	int i,j;

	le=ri=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].r)

			return puts("No"),0;

		le-=a[i].r,le+=a[i].l;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,ri,le;

struct node{

	int l,r;

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int i,j;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].r)

			return puts("No"),0;

		//le-=a[i].y-a[i].x;

		le-=a[i].r,le+=a[i].l;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.216768774056051x):
#include <cstdio>

#include <algorithm>

using namespace std;

struct node{

	int l,r;

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ri,le;

	int i,j;

	le=ri=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].r)

			return puts("No"),0;

		le-=a[i].r,le+=a[i].l;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

ll read(){

	ll a=0,b=getchar(),c=1;

	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();

	while(isdigit(b))a=a*10+b-'0',b=getchar();

	return a*c;

}

const ll mod=998244353;

ll n,ri,le,s0,f[1000005];

struct node{

	int x,y;

	bool operator<(node b)const{

		return x-y>b.x-b.y;

	}

}a[1000005];

char s[1000005];

int main(){

	n=read();

	for(int i=1;i<=n;i++){

		ll l=0,r=0;

		scanf("%s",s+1);

		for(int j=1;s[j];j++)

			if(s[j]=='(')l++;

			else{

				if(l)l--;

				else r++;

			}

		if(!l and r)ri+=r;

		else if(l and !r)le+=l;

		else a[i]=(node){l,r};	

		s0+=l-r;

	}

	if(s0!=0)return puts("No"),0;

	sort(a+1,a+1+n);

	for(int i=1;i<=n;i++){

		if(le<a[i].y)

			return puts("No"),0;

		le-=a[i].y-a[i].x;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.198766627131127x):
#include <cstdio>

#include <algorithm>

#define maxn 1000010

using namespace std;

struct node{

	int l,r;

}a[maxn];

int tot;

char s[maxn];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ln,rn;

	int i,j;

	ln=rn=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)rn+=r;

		else if(l&&!r)ln+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(ln<a[i].r)

			return printf("No\n")*0;

		ln-=a[i].r,ln+=a[i].l;

	}

	if(ln!=rn)return printf("No\n")*0;

	printf("Yes\n");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef int ll;

ll read(){

	ll a=0,b=getchar(),c=1;

	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();

	while(isdigit(b))a=a*10+b-'0',b=getchar();

	return a*c;

}

const ll mod=998244353;

ll n,ri,le,s0,f[1000005];

struct node{

	int x,y;

	bool operator<(node b)const{

		return x-y>b.x-b.y;

	}

}a[1000005];

char s[1000005];

int main(){

	n=read();

	for(int i=1;i<=n;i++){

		ll l=0,r=0;

		scanf("%s",s+1);

		for(int j=1;s[j];j++)

			if(s[j]=='(')l++;

			else{

				if(l)l--;

				else r++;

			}

		if(!l and r)ri+=r;

		else if(l and !r)le+=l;

		else a[i]=(node){l,r};	

		s0+=l-r;

	}

	if(s0!=0)return puts("No"),0;

	sort(a+1,a+1+n);

	for(int i=1;i<=n;i++){

		if(le<a[i].y)

			return puts("No"),0;

		le-=a[i].y-a[i].x;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.199466464760425x):
#include <cstdio>

#include <algorithm>

#define maxn 1000010

using namespace std;

struct node{

	int l,r;

}a[maxn];

int tot;

char s[maxn];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ln,rn;

	int i,j;

	ln=rn=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)rn+=r;

		else if(l&&!r)ln+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(ln<a[i].r)

			return printf("No\n")*0;

		ln-=a[i].r,ln+=a[i].l;

	}

	if(ln!=rn)return printf("No\n")*0;

	printf("Yes\n");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef int ll;

ll read(){

	ll a=0,b=getchar(),c=1;

	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();

	while(isdigit(b))a=a*10+b-'0',b=getchar();

	return a*c;

}

const ll mod=998244353;

ll n,ri,le,s0,f[1000005];

struct node{

	int x,y;

	/*bool operator<(node b)const{

		return x-y>b.x-b.y;

	}*/

}a[1000005];

int tot;

char s[1000005];

int cmp(node a,node b){

	return a.x-a.y>b.x-b.y;

}

int main(){

	n=read();

	for(int i=1;i<=n;i++){

		ll l=0,r=0;

		scanf("%s",s+1);

		for(int j=1;s[j];j++)

			if(s[j]=='(')l++;

			else{

				if(l)l--;

				else r++;

			}

		if(!l&&r)ri+=r;

		else if(l&&!r)le+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

		//s0+=l-r;

	}

	//if(s0!=0)return puts("No"),0;

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(le<a[i].y)

			return puts("No"),0;

		le-=a[i].y-a[i].x;

	}

	if(le!=ri)puts("No");

	else puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.198832871646798x):
#include <cstdio>

#include <algorithm>

#define maxn 1000010

using namespace std;

struct node{

	int l,r;

}a[maxn];

int tot;

char s[maxn];

int cmp(node a,node b){

	return a.l-a.r>b.l-b.r;

}

int main(){

	int n,ln,rn;

	int i,j;

	ln=rn=0;

	scanf("%d",&n);

	for(i=1;i<=n;i++){

		int l=0,r=0;

		scanf("%s",s+1);

		for(j=1;s[j];j++)

			if(s[j]=='(')l++;

			else if(s[j]==')'){

				if(l)l--;

				else r++;

			}

		if(!l&&r)rn+=r;

		else if(l&&!r)ln+=l;

		else if(l&&r)a[++tot]=(node){l,r};	

	}

	sort(a+1,a+1+tot,cmp);

	for(int i=1;i<=tot;i++){

		if(ln<a[i].r)

			return printf("No\n")*0;

		ln-=a[i].r,ln+=a[i].l;

	}

	if(ln!=rn)return printf("No\n")*0;

	printf("Yes\n");

	return 0;

}