stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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");

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define MAX_N 8003



bool can(vector <int> v ,int x){

    bitset <MAX_N> bs; bs[0] = 1;

    int tot = 0;

    for(int&i : v)

        bs |= (bs<<i) ,tot += i;

    return !((tot+x)&1) && (tot >= x && bs[(tot-x)/2]);

}



int main()

{

    string s;

    int x ,y;

    cin >> s; s += 'T';

    cin >> x >> y; y = abs(y);



    vector <int> units[2];

    int axis = 0 ,mvs = 0;

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

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

            units[axis].push_back(mvs);

            axis ^= 1 ,mvs = 0;

        }

        else

            mvs++;

    }



    x = abs(x-units[0][0]);

    units[0].erase(units[0].begin());

    cout << (can(units[0] ,x) && can(units[1] ,y) ? "Yes" : "No") << endl;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;



string s;

int x, y;

int dpx[2][20010], dpy[2][20010];

vector<int> vx, vy;



int main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> s >> x >> y;

  int l = 0, r = 0, turn = 0;

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

  dpx[0][10000+r-l] = 1;

  dpy[0][10000] = 1;

  l = r;

  while (l < s.size()) {

    while (l < s.size() && s[l] == 'T') l++, r++, turn ^= 1;

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

    if (turn == 0) vx.emplace_back(r-l);

    else vy.emplace_back(r-l);

    l = r;

  }

  for (int i = 0; i < vx.size(); i++) {

    fill_n((int*)dpx[1], 20010, 0);

    for (int j = 0; j < 20010; j++) {

      if (j-vx[i] >= 0) dpx[1][j-vx[i]] |= dpx[0][j];

      if (j+vx[i] < 20010) dpx[1][j+vx[i]] |= dpx[0][j];

    }

    swap(dpx[0], dpx[1]);

  }

  for (int i = 0; i < vy.size(); i++) {

    fill_n((int*)dpy[1], 20010, 0);

    for (int j = 0; j < 20010; j++) {

      if (j-vy[i] >= 0) dpy[1][j-vy[i]] |= dpy[0][j];

      if (j+vy[i] < 20010) dpy[1][j+vy[i]] |= dpy[0][j];

    }

    swap(dpy[0], dpy[1]);

  }

  cout << (dpx[0][10000+x] && dpy[0][10000+y] ? "Yes" : "No") << endl;



  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;



string s;

int x, y;

int n;

vector<int> vx, vy;

int fx[2][16010], fy[2][16010];



int main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> s >> x >> y;

  n = s.size();

  int l, r;

  l = 0, r = 0;

  while (l < n && s[l] == 'F') l++, r++, x--;

  int turn = 0;

  while (l < n) {

    while (l < n && s[l] == 'T') l++, r++, turn ^= 1;

    while (r < n && s[r] == 'F') r++;

    if (r != l) {

      if (turn) vy.emplace_back(r-l);

      else vx.emplace_back(r-l);

    }

    l = r;

  }

  fx[0][8000] = 1;

  fy[0][8000] = 1;

  for (int i = 0; i < vx.size(); i++) {

    fill_n((int*)fx[1], 16010, 0);

    for (int j = 0; j < 16010; j++) {

      if (fx[0][j]) {

        fx[1][j-vx[i]] = 1;

        fx[1][j+vx[i]] = 1;

      }

    }

    swap(fx[0], fx[1]);

  }

  for (int i = 0; i < vy.size(); i++) {

    fill_n((int*)fy[1], 16010, 0);

    for (int j = 0; j < 16010; j++) {

      if (fy[0][j]) {

        fy[1][j-vy[i]] = 1;

        fy[1][j+vy[i]] = 1;

      }

    }

    swap(fy[0], fy[1]);

  }

  cout << (fx[0][x+8000] && fy[0][y+8000] ? "Yes" : "No") << endl;



  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using ull = unsigned long long;

using pii = pair<int, int>;

using pll = pair<ll, ll>;

using pli = pair<ll, int>;

using Graph = vector<vector<bool>>;

const int MOD = 1e9 + 7;



bool isreachable(int s, int t, queue<int> &cnt) {

    vector<bool> v(16003);

    v[s + 8000] = true;

    while (!cnt.empty()) {

        int i = cnt.front();

        cnt.pop();

        vector<bool> vtmp(16003);

        for (int j = 0; j <= 16000; ++j) {

            if (j - i >= 0)

                vtmp[j] = v[j - i];

            if (j + i <= 16000)

                vtmp[j] = vtmp[j] | v[j + i];

        }

        swap(v, vtmp);

    }

    return v[t + 8000];

}



int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);



    string s;

    int x, y;

    cin >> s >> x >> y;



    bool h = true;

    int cnt = 0;

    queue<int> hcnt, vcnt;

    for (int i = 0; i < s.length(); ++i) {

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

            (h ? hcnt : vcnt).push(cnt);

            h ^= 1;

            cnt = 0;

            continue;

        }

        cnt++;

    }

    if (cnt > 0)

        (h ? hcnt : vcnt).push(cnt);



    int ix = hcnt.front(), iy = 0;

    hcnt.pop();



    bool isok = isreachable(ix, x, hcnt) & isreachable(iy, y, vcnt);

    cout << (isok ? "Yes" : "No") << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using ull = unsigned long long;

using pii = pair<int, int>;

using pll = pair<ll, ll>;

using pli = pair<ll, int>;

using Graph = vector<vector<bool>>;

const int MOD = 1e9 + 7;



int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);



    string s;

    int x, y;

    cin >> s >> x >> y;

    if (s.back() != 'T')

        s.push_back('T');



    bool v = false, ini = false;

    int cnt = 0;

    vector<bitset<16000>> dp(2);

    dp[1][8000] = true;

    for (int i = 0; i < s.length(); ++i) {

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

            if (!ini) {

                dp[v][8000 + cnt] = true;

                ini = true;

            } else

                dp[v] = (dp[v] >> cnt) | (dp[v] << cnt);

            v ^= 1;

            cnt = 0;

            continue;

        }

        cnt++;

    }



    cout << (dp[0][8000 + x] & dp[1][8000 + y] ? "Yes" : "No") << endl;

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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");

  }

}

// Optimized solution to the same problem (faster):
#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");

  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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");

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem: