prithivMLmods/Coder-Stat huggingface dataset:
Problem p03487 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>

typedef long long ll;
using namespace std;

int main() {
  int n, sum = 0;
  int a[10001] = {0}, tmp, i;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> tmp;
    if (tmp > n)
      sum++;
    else
      a[tmp]++;
  }

  for (i = 1; i <= n; i++) {
    if (a[i] >= i)
      sum += a[i] - i;
    else
      sum += a[i];
  }

  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>

typedef long long ll;
using namespace std;

int main() {
  int n, sum = 0;
  int a[100001] = {0}, tmp, i;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> tmp;
    if (tmp > n)
      sum++;
    else
      a[tmp]++;
  }

  for (i = 1; i <= n; i++) {
    if (a[i] >= i)
      sum += a[i] - i;
    else
      sum += a[i];
  }

  cout << sum << endl;

  return 0;
}
Problem p03487 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  long long int a[10010];
  for (int i = 1; i < 10010; i++) {
    a[i] = 0;
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    long long int x;
    cin >> x;
    if (x > n) {
      ans++;
    } else {
      a[x]++;
    }
  }

  for (int i = 1; i <= n; i++) {
    if (i <= a[i]) {
      ans += a[i] - i;
    } else {
      ans += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  long long int a[100010];
  for (int i = 1; i < 100010; i++) {
    a[i] = 0;
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    long long int x;
    cin >> x;
    if (x > n) {
      ans++;
    } else {
      a[x]++;
    }
  }

  for (int i = 1; i <= n; i++) {
    if (i <= a[i]) {
      ans += a[i] - i;
    } else {
      ans += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03487 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vs = vector<string>;
using vb = vector<bool>;
using vll = vector<long long>;
using vpii = vector<pair<int, int>>;
using vivi = vector<vector<int>>;
using vbvb = vector<vector<bool>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int mod = 1000000007;
const int inf = 1001001001;

int main(void) {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  sort(ALL(a));
  int cnt = 0;
  rep(i, n) {
    int p = a[i];
    auto q = upper_bound(ALL(a), p);
    auto r = lower_bound(ALL(a), p);
    if (q - r >= p)
      cnt += q - r - p;
    else
      cnt += q - r;
    i = q - a.begin() - 1;
  }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vs = vector<string>;
using vb = vector<bool>;
using vll = vector<long long>;
using vpii = vector<pair<int, int>>;
using vivi = vector<vector<int>>;
using vbvb = vector<vector<bool>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int mod = 1000000007;
const int inf = 1001001001;

int main(void) {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  sort(ALL(a));
  int cnt = 0;
  rep(i, n) {
    int p = a[i];
    auto q = upper_bound(ALL(a), p);
    auto r = lower_bound(ALL(a), p);
    if (q - r >= p)
      cnt += q - r - p;
    else
      cnt += q - r;
    i = q - a.begin() - 1;
  }
  cout << cnt << endl;
}
Problem p03487 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int INF = 1e9;

int main() {
  int N;
  cin >> N;

  int ans = 0;

  vector<int> a;
  rep(i, N) {
    int ai;
    cin >> ai;
    if (ai <= N)
      a.push_back(ai);
    else
      ans++;
  }

  if (a.size() == 0) {
    cout << ans << endl;
    return 0;
  }

  sort(a.begin(), a.end());
  a.push_back(INF); // dummy

  int prev = 0;
  for (int i = 1; i <= N; i++) {
    if (a[i - 1] == a[i])
      continue;
    int n = i - prev;
    int m = a[i - 1];
    if (n < m)
      ans += n;
    if (n > m)
      ans += n - m;
    prev = i;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int INF = 1e9;

int main() {
  int N;
  cin >> N;

  int ans = 0;

  vector<int> a;
  rep(i, N) {
    int ai;
    cin >> ai;
    if (ai <= N)
      a.push_back(ai);
    else
      ans++;
  }

  if (a.size() == 0) {
    cout << ans << endl;
    return 0;
  }

  sort(a.begin(), a.end());
  a.push_back(INF); // dummy

  int prev = 0;
  for (int i = 1; i < a.size(); i++) {
    if (a[i - 1] == a[i])
      continue;
    int n = i - prev;
    int m = a[i - 1];
    if (n < m)
      ans += n;
    if (n > m)
      ans += n - m;
    prev = i;
  }

  cout << ans << endl;
}
Problem p03487 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int v[10001];
int main() {
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x > n) {
      ans++;
    } else {
      v[x]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (v[i] < i) {
      ans += v[i];
    } else if (v[i] > i) {
      ans += v[i] - i;
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
int v[100001];
int main() {
  int n;
  scanf("%d", &n);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x > n) {
      ans++;
    } else {
      v[x]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (v[i] < i) {
      ans += v[i];
    } else if (v[i] > i) {
      ans += v[i] - i;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03487 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  scanf("%d", &N);

  vector<int> C(N + 1, 0);
  int tmp;
  for (int i = 1; i < N + 1; i++) {
    scanf("%d", &tmp);
    // printf("[%d]", tmp);
    C[tmp]++;
  }
  int len = 0;
  for (int i = 1; i < N + 1; i++) {
    len += C[i] >= i ? i : 0;
  }
  printf("%d", N - len);
  return 0;

  // vector<int> A(N);
  // for (int i = 0; i < N; i++) {
  // 	scanf("%d", &A[i]);
  // 	// printf("[%d]", A[i]);
  // }

  // int len = 0;
  // for (int i = 0; i < N + 1; i++) {
  // 	// printf("%d, %d\n", i, (int)count(A.begin(), A.end(), i));
  // 	len += (int)count(A.begin(), A.end(), i) >= i ? i : 0;
  // }

  // printf("%d", N - len);
  // return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  scanf("%d", &N);

  vector<int> C(N + 1, 0);
  int tmp;
  for (int i = 1; i < N + 1; i++) {
    scanf("%d", &tmp);
    // printf("[%d]", tmp);
    if (tmp <= N) {
      C[tmp]++;
    }
  }
  int len = 0;
  for (int i = 1; i < N + 1; i++) {
    len += C[i] >= i ? i : 0;
  }
  printf("%d", N - len);
  return 0;

  // vector<int> A(N);
  // for (int i = 0; i < N; i++) {
  // 	scanf("%d", &A[i]);
  // 	// printf("[%d]", A[i]);
  // }

  // int len = 0;
  // for (int i = 0; i < N + 1; i++) {
  // 	// printf("%d, %d\n", i, (int)count(A.begin(), A.end(), i));
  // 	len += (int)count(A.begin(), A.end(), i) >= i ? i : 0;
  // }

  // printf("%d", N - len);
  // return 0;
}
Problem p03487 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
using namespace std;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  ll a[100010];
  unordered_map<ll, ll> b;
  for (ll i = 1; i <= N; i++) {
    cin >> a[i];
    b[a[i]]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= 1e9; i++) {
    if (b[i] > 0 && b[i] < i)
      ans += b[i];
    else if (b[i] > i)
      ans += b[i] - i;
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
using namespace std;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  ll a[100010];
  unordered_map<ll, ll> b;
  for (ll i = 1; i <= N; i++) {
    cin >> a[i];
    b[a[i]]++;
  }
  ll ans = 0;
  unordered_map<long long, bool> z;
  for (ll i = 1; i <= N; i++) {
    if (z[a[i]] == false) {
      if (b[a[i]] > 0 && b[a[i]] < a[i])
        ans += b[a[i]];
      else if (b[a[i]] > a[i])
        ans += b[a[i]] - a[i];
    }
    z[a[i]] = true;
  }
  cout << ans;
}
Problem p03487 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
int n, a[10010], s, g;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  s = 1;
  for (int i = 2; i <= n; i++)
    if (a[i] != a[i - 1]) {
      if (s > a[i - 1])
        g += (s - a[i - 1]);
      if (s < a[i - 1])
        g += s;
      s = 1;
    } else
      s++;
  if (s > a[n])
    g += (s - a[n]);
  if (s < a[n])
    g += s;
  cout << g << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
int n, a[1000010], s, g;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  s = 1;
  for (int i = 2; i <= n; i++)
    if (a[i] != a[i - 1]) {
      if (s > a[i - 1])
        g += (s - a[i - 1]);
      if (s < a[i - 1])
        g += s;
      s = 1;
    } else
      s++;
  if (s > a[n])
    g += (s - a[n]);
  if (s < a[n])
    g += s;
  cout << g << endl;
  return 0;
}
Problem p03487 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
const int MaxN = 1e5;
int vis[MaxN + 5];
int b[MaxN + 5];
int n, c, a;
long long ans;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    vis[a] = 1;
    b[a]++;
    if (c < a)
      c = a;
  }
  for (int j = 1; j <= c; j++) {
    if (vis[j] == 0)
      continue;
    if (b[j] > j)
      ans = ans + b[j] - j;
    if (b[j] < j)
      ans = ans + b[j];
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
const int MaxN = 1e5;
int vis[MaxN + 5];
int b[MaxN + 5];
int n, c, a;
long long ans;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    if (a > MaxN) {
      ans++;
      continue;
    }
    vis[a] = 1;
    b[a]++;
    if (c < a)
      c = a;
  }
  for (int j = 1; j <= c; j++) {
    if (vis[j] == 0)
      continue;
    if (b[j] > j)
      ans = ans + b[j] - j;
    if (b[j] < j)
      ans = ans + b[j];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <cstdio>
#include <numeric>
#include <string>
#include <vector>

bool ok(std::vector<int> const &xs, int x) {
  std::bitset<8001> dp;
  dp.set(0);
  for (auto xi : xs)
    dp |= dp << xi;
  return dp[x];
}

int main() {
  char buf[8192];
  scanf("%s", buf);
  std::string s = buf;

  std::vector<int> hz{0}, vt; // 水平方向、垂直方向の移動回数
  bool see_hz = true;
  for (auto c : s) {
    if (c == 'T') {
      see_hz = !see_hz;
      (see_hz ? hz : vt).push_back(0);
    } else {
      ++(see_hz ? hz : vt).back();
    }
  }

  int x, y;
  scanf("%d %d", &x, &y);

  // 最初の方向は確定しているのでずらす。
  x -= hz[0];
  hz.erase(hz.begin());

  // hz の部分和 s で、s - (sum(hz)-s) = x なるものが作れればよい。
  // 2s = x+sum(hz); s = (x+sum(hz))/2 と同値。なので、x+sum(hz) が奇数なら No。
  int sum_hz = std::accumulate(hz.begin(), hz.end(), 0);
  int sum_vt = std::accumulate(vt.begin(), vt.end(), 0);
  if ((sum_hz + x) % 2 != 0 || (sum_vt + y) % 2 != 0)
    return puts("No"), 0;

  // x, y の各方向について所望の部分和が作れればよい。
  puts((ok(hz, (x + sum_hz) / 2) && ok(vt, (y + sum_vt) / 2)) ? "Yes" : "No");
}


Fixed submission:
#include <bitset>
#include <cstdio>
#include <numeric>
#include <string>
#include <vector>

bool ok(std::vector<int> const &xs, int x) {
  if (x < 0)
    return false;
  std::bitset<8001> dp;
  dp.set(0);
  for (auto xi : xs)
    dp |= dp << xi;
  return dp[x];
}

int main() {
  char buf[8192];
  scanf("%s", buf);
  std::string s = buf;

  std::vector<int> hz{0}, vt; // 水平方向、垂直方向の移動回数
  bool see_hz = true;
  for (auto c : s) {
    if (c == 'T') {
      see_hz = !see_hz;
      (see_hz ? hz : vt).push_back(0);
    } else {
      ++(see_hz ? hz : vt).back();
    }
  }

  int x, y;
  scanf("%d %d", &x, &y);

  // 最初の方向は確定しているのでずらす。
  x -= hz[0];
  hz.erase(hz.begin());

  // hz の部分和 s で、s - (sum(hz)-s) = x なるものが作れればよい。
  // 2s = x+sum(hz); s = (x+sum(hz))/2 と同値。なので、x+sum(hz) が奇数なら No。
  int sum_hz = std::accumulate(hz.begin(), hz.end(), 0);
  int sum_vt = std::accumulate(vt.begin(), vt.end(), 0);
  if ((sum_hz + x) % 2 != 0 || (sum_vt + y) % 2 != 0)
    return puts("No"), 0;

  // x, y の各方向について所望の部分和が作れればよい。
  puts((ok(hz, (x + sum_hz) / 2) && ok(vt, (y + sum_vt) / 2)) ? "Yes" : "No");
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int a[4005], dp[8005][16005], k;
void DP(int x) {
  dp[x][a[x] + 8000] = 1;
  for (int i = x + 2; i <= k; i += 2)
    for (int j = 0; j <= 16000; j++) {
      if (j + a[i] <= 16000)
        dp[i][j] = dp[i - 2][j + a[i]];
      if (j - a[i] >= 0)
        dp[i][j] = dp[i][j] || dp[i - 2][j - a[i]];
    }
}
int main() {
  string s;
  int x, y, n, w = 0, ans;
  cin >> s >> x >> y;
  n = s.size();
  for (int i = 0; i < n; i++) {
    if (s[i] == 'F')
      w++;
    else {
      a[++k] = w;
      w = 0;
    }
  }
  a[++k] = w;
  DP(1);
  DP(0);
  if (k % 2 == 0)
    ans = dp[k][y + 8000] && dp[k - 1][x + 8000];
  else
    ans = dp[k][x + 8000] && dp[k - 1][y + 8000];
  if (ans == 1)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int a[8005], dp[8005][16005], k;
void DP(int x) {
  dp[x][a[x] + 8000] = 1;
  for (int i = x + 2; i <= k; i += 2)
    for (int j = 0; j <= 16000; j++) {
      if (j + a[i] <= 16000)
        dp[i][j] = dp[i - 2][j + a[i]];
      if (j - a[i] >= 0)
        dp[i][j] = dp[i][j] || dp[i - 2][j - a[i]];
    }
}
int main() {
  string s;
  int x, y, n, w = 0, ans;
  cin >> s >> x >> y;
  n = s.size();
  for (int i = 0; i < n; i++) {
    if (s[i] == 'F')
      w++;
    else {
      a[++k] = w;
      w = 0;
    }
  }
  a[++k] = w;
  DP(1);
  DP(0);
  if (k % 2 == 0)
    ans = dp[k][y + 8000] && dp[k - 1][x + 8000];
  else
    ans = dp[k][x + 8000] && dp[k - 1][y + 8000];
  if (ans == 1)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  int x, y;
  cin >> s >> x >> y;

  int count = 0, way = 0;
  vector<int> a, b;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == 'F')
      count++;
    else {
      if (way == 0)
        a.emplace_back(count);
      else
        b.emplace_back(count);
      count = 0;
      way ^= 1;
    }
  }
  if (way == 0)
    a.emplace_back(count);
  else
    b.emplace_back(count);

  vector<vector<int>> px(a.size(), vector<int>(16001, 0)),
      py(b.size(), vector<int>(16001, 0));
  for (int i = 0; i < a.size(); i++) {
    if (i == 0 && s.at(0) == 'F')
      px[i].at(8000 + a.at(0)) = 1;
    else if (i == 0 && s.at(0) == 'T')
      px[i].at(8000) = 1;
    else {
      for (int j = 0; j < 16001; j++) {
        if (px[i - 1].at(j) == 1) {
          px[i].at(j + a.at(i)) = 1;
          px[i].at(j - a.at(i)) = 1;
        }
      }
    }
  }
  for (int i = 0; i < b.size(); i++) {
    if (i == 0)
      py[i].at(8000 + b.at(i)) = py[i].at(8000 - b.at(i)) = 1;
    else {
      for (int j = 0; j < 16001; j++) {
        if (py[i - 1].at(j) == 1) {
          py[i].at(j + b.at(i)) = 1;
          py[i].at(j - b.at(i)) = 1;
        }
      }
    }
  }

  if (px.back().at(8000 + x) == 1 && py.back().at(8000 + y) == 1)
    cout << "Yes\n";
  else
    cout << "No\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  int x, y;
  cin >> s >> x >> y;

  int count = 0, way = 0;
  vector<int> a, b;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == 'F')
      count++;
    else {
      if (way == 0)
        a.emplace_back(count);
      else
        b.emplace_back(count);
      count = 0;
      way ^= 1;
    }
  }
  if (way == 0)
    a.emplace_back(count);
  else
    b.emplace_back(count);
  a.emplace_back(0);
  b.emplace_back(0);

  vector<vector<int>> px(a.size(), vector<int>(16001, 0)),
      py(b.size(), vector<int>(16001, 0));
  for (int i = 0; i < a.size(); i++) {
    if (i == 0 && s.at(0) == 'F')
      px[i].at(8000 + a.at(0)) = 1;
    else if (i == 0 && s.at(0) == 'T')
      px[i].at(8000) = 1;
    else {
      for (int j = 0; j < 16001; j++) {
        if (px[i - 1].at(j) == 1) {
          px[i].at(j + a.at(i)) = 1;
          px[i].at(j - a.at(i)) = 1;
        }
      }
    }
  }
  for (int i = 0; i < b.size(); i++) {
    if (i == 0)
      py[i].at(8000 + b.at(i)) = py[i].at(8000 - b.at(i)) = 1;
    else {
      for (int j = 0; j < 16001; j++) {
        if (py[i - 1].at(j) == 1) {
          py[i].at(j + b.at(i)) = 1;
          py[i].at(j - b.at(i)) = 1;
        }
      }
    }
  }

  if (px.back().at(8000 + x) == 1 && py.back().at(8000 + y) == 1)
    cout << "Yes\n";
  else
    cout << "No\n";
}
Problem p03488 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict

s = input()
x, y = map(int, input().split())

arr = list(map(len, s.split("T")))
EW = [arr[ew] for ew in range(0, len(arr), 2)]
NS = [arr[ns] for ns in range(1, len(arr), 2)]

# dp[i][j] := i回目までの移動で座標jにいることができるか
dp = [defaultdict(bool) for i in range(len(EW))]
dp[0][EW[0]] = True
for i in range(1, len(EW)):
    for prev in dp[i - 1].keys():
        dp[i][prev + EW[i]] = True
        dp[i][prev - EW[i]] = True
if not dp[-1][x]:
    print("No")
    exit(0)

dp = [defaultdict(bool) for i in range(len(NS))]
dp[0][NS[0]] = True
dp[0][-NS[0]] = True
for i in range(1, len(NS)):
    for prev in dp[i - 1].keys():
        dp[i][prev + NS[i]] = True
        dp[i][prev - NS[i]] = True
if not dp[-1][y]:
    print("No")
    exit(0)

print("Yes")


Fixed submission:
from collections import defaultdict

s = input()
x, y = map(int, input().split())

arr = list(map(len, s.split("T")))
EW = [arr[ew] for ew in range(0, len(arr), 2)]
NS = [arr[ns] for ns in range(1, len(arr), 2)]

# dp[i][j] := i回目までの移動で座標jにいることができるか
dp = [defaultdict(bool) for i in range(len(EW))]
dp[0][EW[0]] = True
for i in range(1, len(EW)):
    for prev in dp[i - 1].keys():
        dp[i][prev + EW[i]] = True
        dp[i][prev - EW[i]] = True
if not dp[-1][x]:
    print("No")
    exit(0)

if len(NS) > 0:
    dp = [defaultdict(bool) for i in range(len(NS))]
    dp[0][NS[0]] = True
    dp[0][-NS[0]] = True
    for i in range(1, len(NS)):
        for prev in dp[i - 1].keys():
            dp[i][prev + NS[i]] = True
            dp[i][prev - NS[i]] = True
    if not dp[-1][y]:
        print("No")
        exit(0)

print("Yes")
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> dx(2010, 0), dy(2010, 0);

int main() {
  string s;
  int x, y;

  cin >> s >> x >> y;

  int l = s.size();
  int tcnt = 0, j = 1, k = 1;
  for (int i = 0; i < l; i++) {
    if (s[i] == 'F') {
      if (tcnt % 2 == 0) {
        dx[j]++;
      } else {
        dy[k]++;
      }
    } else {
      if (tcnt % 2 == 0) {
        if (dx[j])
          j++;
      } else {
        if (dy[k])
          k++;
      }
      tcnt++;
    }
  }

  vector<vector<bool>> dpx(j + 10, vector<bool>(2 * l + 10, false));
  vector<vector<bool>> dpy(k + 10, vector<bool>(2 * l + 10, false));

  if (s[0] == 'F') {
    dpx[1][l + dx[1]] = true;
  } else {
    dpx[1][l + dx[1]] = true, dpx[1][l - dx[1]] = true;
  }
  int i;
  for (i = 2; dx[i] > 0; i++) {
    for (int j = dx[i]; j + dx[i] <= 2 * l; j++) {
      dpx[i][j] = dpx[i - 1][j - dx[i]] || dpx[i - 1][j + dx[i]];
    }
  }
  int ansx = i - 1;

  dpy[1][l + dy[1]] = true, dpy[1][l - dy[1]] = true;

  for (i = 2; dy[i] > 0; i++) {
    for (int j = dy[i]; j + dy[i] <= 2 * l; j++) {
      dpy[i][j] = dpy[i - 1][j - dy[i]] || dpy[i - 1][j + dy[i]];
    }
  }
  int ansy = i - 1;

  if (dpx[ansx][x + l] && dpy[ansy][y + l]) {
    cout << "Yes" << endl;
    return 0;
  }
  cout << "No" << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> dx(4010, 0), dy(4010, 0);

int main() {
  string s;
  int x, y;

  cin >> s >> x >> y;

  int l = s.size();
  int tcnt = 0, j = 1, k = 1;
  for (int i = 0; i < l; i++) {
    if (s[i] == 'F') {
      if (tcnt % 2 == 0) {
        dx[j]++;
      } else {
        dy[k]++;
      }
    } else {
      if (tcnt % 2 == 0) {
        if (dx[j])
          j++;
      } else {
        if (dy[k])
          k++;
      }
      tcnt++;
    }
  }

  vector<vector<bool>> dpx(j + 10, vector<bool>(2 * l + 10, false));
  vector<vector<bool>> dpy(k + 10, vector<bool>(2 * l + 10, false));

  if (s[0] == 'F') {
    dpx[1][l + dx[1]] = true;
  } else {
    dpx[1][l + dx[1]] = true, dpx[1][l - dx[1]] = true;
  }
  int i;
  for (i = 2; dx[i] > 0; i++) {
    for (int j = dx[i]; j + dx[i] <= 2 * l; j++) {
      dpx[i][j] = dpx[i - 1][j - dx[i]] || dpx[i - 1][j + dx[i]];
    }
  }
  int ansx = i - 1;

  dpy[1][l + dy[1]] = true, dpy[1][l - dy[1]] = true;

  for (i = 2; dy[i] > 0; i++) {
    for (int j = dy[i]; j + dy[i] <= 2 * l; j++) {
      dpy[i][j] = dpy[i - 1][j - dy[i]] || dpy[i - 1][j + dy[i]];
    }
  }
  int ansy = i - 1;

  if (dpx[ansx][x + l] && dpy[ansy][y + l]) {
    cout << "Yes" << endl;
    return 0;
  }
  cout << "No" << endl;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool x_dp[3001][50000];
bool y_dp[3001][50000];

int main() {
  string s;
  int X, Y;
  cin >> s >> X >> Y;
  vector<int> cnt;
  int temp = 0;
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == 'F') {
      ++temp;
    } else {
      cnt.push_back(temp);
      temp = 0;
    }
  }
  cnt.push_back(temp);

  int n = cnt.size();

  const int dif = 25000;
  x_dp[0][cnt[0] + dif] = true;
  y_dp[0][dif] = true;

  int yn = n / 2;
  int xn = n - yn;

  // 原点を25000とする
  for (int i = 1; i < xn; ++i) {
    int d = cnt[2 * i];
    for (int x = -8000; x <= 8000; ++x) {
      x_dp[i][x + dif] = x_dp[i - 1][x - d + dif] || x_dp[i - 1][x + d + dif];
    }
  }
  for (int i = 0; i < yn; ++i) {
    int d = cnt[2 * i + 1];
    for (int y = -8000; y <= 8000; ++y) {
      y_dp[i + 1][y + dif] = y_dp[i][y - d + dif] || y_dp[i][y + d + dif];
    }
  }

  if (x_dp[xn - 1][X + dif] && y_dp[yn][Y + dif]) {
    cout << "Yes\n";
  } else {
    cout << "No\n";
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool x_dp[4001][50000];
bool y_dp[4001][50000];

int main() {
  string s;
  int X, Y;
  cin >> s >> X >> Y;
  vector<int> cnt;
  int temp = 0;
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == 'F') {
      ++temp;
    } else {
      cnt.push_back(temp);
      temp = 0;
    }
  }
  cnt.push_back(temp);

  int n = cnt.size();

  const int dif = 25000;
  x_dp[0][cnt[0] + dif] = true;
  y_dp[0][dif] = true;

  int yn = n / 2;
  int xn = n - yn;

  // 原点を25000とする
  for (int i = 1; i < xn; ++i) {
    int d = cnt[2 * i];
    for (int x = -8000; x <= 8000; ++x) {
      x_dp[i][x + dif] = x_dp[i - 1][x - d + dif] || x_dp[i - 1][x + d + dif];
    }
  }
  for (int i = 0; i < yn; ++i) {
    int d = cnt[2 * i + 1];
    for (int y = -8000; y <= 8000; ++y) {
      y_dp[i + 1][y + dif] = y_dp[i][y - d + dif] || y_dp[i][y + d + dif];
    }
  }

  if (x_dp[xn - 1][X + dif] && y_dp[yn][Y + dif]) {
    cout << "Yes\n";
  } else {
    cout << "No\n";
  }
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 5000;
string s;
int X, Y;
vector<int> v[2];
bool f[N][N];

bool check(vector<int> &a, int M) {
  // for(auto x: a) cout << x << ' '; cout << endl << M << endl;
  if (M < 0)
    return false;
  else if (M == 0)
    return true;
  else if (M % 2)
    return false;
  M /= 2;
  int n = a.size();
  a.insert(a.begin(), 0);
  for (int i = 1; i <= M; i++)
    for (int j = 1; j <= n; j++)
      f[i][j] = false;
  for (int j = 0; j <= n; j++)
    f[0][j] = true;

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= n; j++) {
      f[i][j] = f[i][j - 1];
      if (a[j] <= i)
        f[i][j] = f[i][j] || f[i - a[j]][j - 1];
    }
  }
  return f[M][n];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s >> X >> Y;

  int initialX = 0, i = 0;
  for (; i < s.length() && s[i] == 'F'; i++)
    initialX++;

  int cur = 0;
  for (int j = i; j < s.length(); j++)
    if (s[j] == 'T')
      cur = 1 - cur, v[cur].push_back(0);
    else
      v[cur].back()++;

  int sx = 0, sy = 0;
  for (auto x : v[0])
    sx += x;
  for (auto y : v[1])
    sy += y;
  bool ok1 = check(v[0], initialX + sx - X);
  bool ok2 = check(v[1], sy - Y);

  cout << (ok1 && ok2 ? "Yes" : "No");

  // for(auto x: v[0]) cout << x << ' '; cout << endl;
  // for(auto x: v[1]) cout << x << ' '; cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 8008;
string s;
int X, Y;
vector<int> v[2];
bool f[N][N];

bool check(vector<int> &a, int M) {
  // for(auto x: a) cout << x << ' '; cout << endl << M << endl;
  if (M < 0)
    return false;
  else if (M == 0)
    return true;
  else if (M % 2)
    return false;
  M /= 2;
  int n = a.size();
  a.insert(a.begin(), 0);
  for (int i = 1; i <= M; i++)
    for (int j = 1; j <= n; j++)
      f[i][j] = false;
  for (int j = 0; j <= n; j++)
    f[0][j] = true;

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= n; j++) {
      f[i][j] = f[i][j - 1];
      if (a[j] <= i)
        f[i][j] = f[i][j] || f[i - a[j]][j - 1];
    }
  }
  return f[M][n];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s >> X >> Y;

  int initialX = 0, i = 0;
  for (; i < s.length() && s[i] == 'F'; i++)
    initialX++;

  int cur = 0;
  for (int j = i; j < s.length(); j++)
    if (s[j] == 'T')
      cur = 1 - cur, v[cur].push_back(0);
    else
      v[cur].back()++;

  int sx = 0, sy = 0;
  for (auto x : v[0])
    sx += x;
  for (auto y : v[1])
    sy += y;
  bool ok1 = check(v[0], initialX + sx - X);
  bool ok2 = check(v[1], sy - Y);

  cout << (ok1 && ok2 ? "Yes" : "No");

  // for(auto x: v[0]) cout << x << ' '; cout << endl;
  // for(auto x: v[1]) cout << x << ' '; cout << endl;
}
Problem p03488 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#include <ios>

using namespace std;

int p_offset = 8200;

bool solve(vector<int> &va, int offset, int p, vector<vector<int>> &memo) {
  if (offset == va.size()) {
    return p == 0;
  }

  if (memo[offset][p + p_offset] == -1) {
    bool ans1, ans2;
    ans1 = solve(va, offset + 1, p - va[offset], memo);
    ans2 = solve(va, offset + 1, p + va[offset], memo);
    memo[offset][p + p_offset] = ans1 || ans2;
  }

  return memo[offset][p + p_offset];
}

int main(int argc, const char *argv[]) {
  string s;
  int x, y;
  cin >> s >> x >> y;

  s += 'T';

  int cnt = 0;
  bool is_x = true, skip = true;
  vector<int> x_mov, y_mov;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'F') {
      cnt++;
      continue;
    }

    if (is_x) {
      x_mov.push_back(cnt);
    } else {
      y_mov.push_back(cnt);
    }

    is_x = !is_x;
    cnt = 0;
  }

  vector<vector<int>> memo1(x_mov.size() + 1, vector<int>(p_offset * 2, -1)),
      memo2(y_mov.size() + 1, vector<int>(p_offset * 2, -1));
  bool ans_x, ans_y;

  ans_x = solve(x_mov, 1, x - x_mov.front(), memo1);
  ans_y = solve(y_mov, 0, y, memo2);

  if (ans_x && ans_y) {
    cout << "Yes" << '\n';
  } else {
    cout << "No" << '\n';
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ios>

using namespace std;

int p_offset = 8150;

bool solve(vector<int> &va, int offset, int p, vector<vector<int>> &memo) {
  if (offset == va.size()) {
    return p == 0;
  }

  if (memo[offset][p + p_offset] == -1) {
    bool ans1, ans2;
    ans1 = solve(va, offset + 1, p - va[offset], memo);
    ans2 = solve(va, offset + 1, p + va[offset], memo);
    memo[offset][p + p_offset] = ans1 || ans2;
  }

  return memo[offset][p + p_offset];
}

int main(int argc, const char *argv[]) {
  string s;
  int x, y;
  cin >> s >> x >> y;

  s += 'T';

  int cnt = 0;
  bool is_x = true, skip = true;
  vector<int> x_mov, y_mov;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'F') {
      cnt++;
      continue;
    }

    if (is_x) {
      x_mov.push_back(cnt);
    } else {
      y_mov.push_back(cnt);
    }

    is_x = !is_x;
    cnt = 0;
  }

  vector<vector<int>> memo1(x_mov.size() + 1, vector<int>(p_offset * 2, -1)),
      memo2(y_mov.size() + 1, vector<int>(p_offset * 2, -1));
  bool ans_x, ans_y;

  ans_x = solve(x_mov, 1, x - x_mov.front(), memo1);
  ans_y = solve(y_mov, 0, y, memo2);

  if (ans_x && ans_y) {
    cout << "Yes" << '\n';
  } else {
    cout << "No" << '\n';
  }

  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int MAX = 8e+3;

int main() {
  string s;
  int x, y;
  cin >> s;
  cin >> x >> y;

  // x 方向、y方向を独立に計算できる
  int count = 0;
  vector<int> X, Y{0}; // x, y 方向それぞれに進む回数
  for (int i = 0; i < (int)s.size(); ++i) {
    if (s[i] == 'T') {
      (X.size() != Y.size() ? X : Y).push_back(count);
      count = 0;
    } else
      ++count;
  }
  (X.size() != Y.size() ? X : Y).push_back(count);

  // dpx[i][j]: i 回目の移動の後に j に居られるか
  bool dpx[MAX][MAX * 2 + 1] = {}; // 座標に +- があるので2倍しておく
  dpx[X[0] + MAX][MAX] = true; // 最初は +x 方向しか行けないことに注意！
  for (int i = 1; i < X.size(); ++i) {
    for (int j = 0; j <= MAX * 2; ++j) {
      if (dpx[i - 1][j] == true) {
        if (j + X[i] <= MAX * 2)
          dpx[i][j + X[i]] = true;
        if (j - X[i] >= 0)
          dpx[i][j - X[i]] = true;
      }
    }
  }

  // y方向についても同様
  bool dpy[MAX][MAX * 2 + 1] = {};
  dpy[0][MAX] = true;
  for (int i = 1; i < Y.size(); ++i) {
    for (int j = 0; j <= MAX * 2; ++j) {
      if (dpy[i - 1][j] == true) {
        if (j + Y[i] <= MAX * 2)
          dpy[i][j + Y[i]] = true;
        if (j - Y[i] >= 0)
          dpy[i][j - Y[i]] = true;
      }
    }
  }

  if (dpx[X.size() - 1][x + MAX] && dpy[Y.size() - 1][y + MAX])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int MAX = 8e+3;

int main() {
  string s;
  int x, y;
  cin >> s;
  cin >> x >> y;

  // x 方向、y方向を独立に計算できる
  int count = 0;
  vector<int> X, Y{0}; // x, y 方向それぞれに進む回数
  for (int i = 0; i < (int)s.size(); ++i) {
    if (s[i] == 'T') {
      (X.size() != Y.size() ? X : Y).push_back(count);
      count = 0;
    } else
      ++count;
  }
  (X.size() != Y.size() ? X : Y).push_back(count);

  // dpx[i][j]: i 回目の移動の後に j に居られるか
  bool dpx[MAX][MAX * 2 + 1] = {}; // 座標に +- があるので2倍しておく
  dpx[0][X[0] + MAX] = true; // 最初は +x 方向しか行けないことに注意！
  for (int i = 1; i < X.size(); ++i) {
    for (int j = 0; j <= MAX * 2; ++j) {
      if (dpx[i - 1][j] == true) {
        if (j + X[i] <= MAX * 2)
          dpx[i][j + X[i]] = true;
        if (j - X[i] >= 0)
          dpx[i][j - X[i]] = true;
      }
    }
  }

  // y方向についても同様
  bool dpy[MAX][MAX * 2 + 1] = {};
  dpy[0][MAX] = true;
  for (int i = 1; i < Y.size(); ++i) {
    for (int j = 0; j <= MAX * 2; ++j) {
      if (dpy[i - 1][j] == true) {
        if (j + Y[i] <= MAX * 2)
          dpy[i][j + Y[i]] = true;
        if (j - Y[i] >= 0)
          dpy[i][j - Y[i]] = true;
      }
    }
  }

  if (dpx[X.size() - 1][x + MAX] && dpy[Y.size() - 1][y + MAX])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

typedef long long ll;
typedef std::pair<ll, ll> P;

constexpr ll INF = 1e15;
constexpr ll mod = 1e9 + 7;

#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  string s;
  cin >> s;
  ll x, y;
  cin >> x >> y;
  ll fx = 0;
  vector<ll> xys[2];
  int n = s.size();
  bool init = false;
  int direction = 0;
  rep(i, n) {
    bool r = s[i] == 'T';
    if (r) {
      init = true;
      direction++;
      direction %= 2;
      xys[direction].push_back(0);
    } else {
      if (init) {
        xys[direction].back()++;
      } else {
        fx++;
      }
    }
  }
  x -= fx;
  bitset<20000> xbit, ybit;
  constexpr int center = 8001;
  xbit.set(center);
  ybit.set(center);
  for (auto p : xys[0]) {
    auto plus = xbit << p;
    auto minus = xbit >> p;
    xbit = plus | minus;
  }
  for (auto p : xys[1]) {
    auto plus = ybit << p;
    auto minus = ybit >> p;
    ybit = plus | minus;
  }
  if (xbit.test(x + center) && ybit.test(y + center)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

typedef long long ll;
typedef std::pair<ll, ll> P;

constexpr ll INF = 1e15;
constexpr ll mod = 1e9 + 7;

#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  string s;
  cin >> s;
  ll x, y;
  cin >> x >> y;
  ll fx = 0;
  vector<ll> xys[2];
  int n = s.size();
  bool init = false;
  int direction = 0;
  rep(i, n) {
    bool r = s[i] == 'T';
    if (r) {
      init = true;
      direction++;
      direction %= 2;
      xys[direction].push_back(0);
    } else {
      if (init) {
        xys[direction].back()++;
      } else {
        fx++;
      }
    }
  }
  x -= fx;
  bitset<32000> xbit, ybit;
  constexpr int center = 16000;
  xbit.set(center);
  ybit.set(center);
  for (auto p : xys[0]) {
    auto plus = xbit << p;
    auto minus = xbit >> p;
    xbit = plus | minus;
  }
  for (auto p : xys[1]) {
    auto plus = ybit << p;
    auto minus = ybit >> p;
    ybit = plus | minus;
  }
  if (xbit.test(x + center) && ybit.test(y + center)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

int dp[4444][8888 * 2][2];

int main(void) {
  ll i, j, k;

  string s;
  cin >> s;
  ll N = s.size();
  ll x, y;
  cin >> x >> y;

  vector<ll> v;
  v.push_back(-1);
  for (i = 0; i < N; i++)
    if (s[i] == 'T')
      v.push_back(i);
  v.push_back(N);

  vector<ll> d[2];
  for (i = 1; i < (ll)v.size() - 1; i++)
    d[i & 1].push_back(v[i + 1] - v[i] - 1);

  dp[0][8000 + (v[1] - v[0] - 1)][0] = 1;
  dp[0][8000][1] = 1;

  ll L[2];
  L[0] = d[0].size();
  L[1] = d[1].size();

  for (ll r = 0; r < 2; r++) {
    for (i = 0; i < L[r]; i++) {
      ll t = d[r][i];
      for (j = 0; j < 8080 * 2; j++) {
        if (j + t < 8080 * 2)
          chmax(dp[i + 1][j + t][r], dp[i][j][r]);
        if (j - t >= 0)
          chmax(dp[i + 1][j - t][r], dp[i][j][r]);
      }
    }
  }

  pt((dp[L[0]][8000 + x][0] && dp[L[1]][8000 + y][1] ? "Yes" : "No"));
}


Fixed submission:
#include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

bool dp[4444][8888 * 2][2];

int main(void) {
  ll i, j, k;

  string s;
  cin >> s;
  ll N = s.size();
  ll x, y;
  cin >> x >> y;

  vector<ll> v;
  v.push_back(-1);
  for (i = 0; i < N; i++)
    if (s[i] == 'T')
      v.push_back(i);
  v.push_back(N);

  vector<ll> d[2];
  for (i = 1; i < (ll)v.size() - 1; i++)
    d[i & 1].push_back(v[i + 1] - v[i] - 1);

  dp[0][8000 + (v[1] - v[0] - 1)][0] = 1;
  dp[0][8000][1] = 1;

  ll L[2];
  L[0] = d[0].size();
  L[1] = d[1].size();

  for (ll r = 0; r < 2; r++) {
    for (i = 0; i < L[r]; i++) {
      ll t = d[r][i];
      for (j = 0; j < 8080 * 2; j++) {
        if (j + t < 8080 * 2)
          chmax(dp[i + 1][j + t][r], dp[i][j][r]);
        if (j - t >= 0)
          chmax(dp[i + 1][j - t][r], dp[i][j][r]);
      }
    }
  }

  pt((dp[L[0]][8000 + x][0] && dp[L[1]][8000 + y][1] ? "Yes" : "No"));
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

set<int> x_dp[8000], y_dp[8000];
vector<int> x_move, y_move;

int main() {

  string s;

  cin >> s;

  bool is_x = true;

  int count = 0;

  s.push_back('T');

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'T') {
      if (is_x) {
        x_move.push_back(count);
      } else { //! is_x
        y_move.push_back(count);
      }
      count = 0;
      is_x = !is_x;
    } else { // s[i] == 'F'
      count++;
    }
  }

  if (s[0] == 'F' && x_move.size() != 0) {
    x_dp[0].insert(x_move[0]);
  } else if (x_move.size() != 0) {
    x_dp[0].insert(x_move[0]);
    x_dp[0].insert(-x_move[0]);
  }

  if (y_move.size() != 0) {
    y_dp[0].insert(y_move[0]);
    y_dp[0].insert(-y_move[0]);
  }

  for (int i = 1; i < x_move.size(); i++) {
    for (auto itr_num : x_dp[i - 1]) {
      x_dp[i].insert(itr_num + x_move[i]);
      x_dp[i].insert(itr_num - x_move[i]);
    }
  }

  for (int i = 1; i < y_move.size(); i++) {
    for (auto itr_num : y_dp[i - 1]) {
      y_dp[i].insert(itr_num + y_move[i]);
      y_dp[i].insert(itr_num - y_move[i]);
    }
  }

  int goal_x, goal_y;

  cin >> goal_x >> goal_y;

  string ans = "No";

  if (x_dp[x_move.size() - 1].find(goal_x) != x_dp[x_move.size() - 1].end() &&
      y_dp[y_move.size() - 1].find(goal_y) != y_dp[y_move.size() - 1].end()) {
    ans = "Yes";
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

set<int> x_dp[8000], y_dp[8000];
vector<int> x_move, y_move;

int main() {

  string s;

  cin >> s;

  bool is_x = true;

  int count = 0;

  s.push_back('T');

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'T') {
      if (is_x) {
        x_move.push_back(count);
      } else { //! is_x
        y_move.push_back(count);
      }
      count = 0;
      is_x = !is_x;
    } else { // s[i] == 'F'
      count++;
    }
  }

  if (is_x) {
    x_move.push_back(count);
  } else { //! is_x
    y_move.push_back(count);
  }

  if (s[0] == 'F' && x_move.size() != 0) {
    x_dp[0].insert(x_move[0]);
  } else if (x_move.size() != 0) {
    x_dp[0].insert(x_move[0]);
    x_dp[0].insert(-x_move[0]);
  }

  if (y_move.size() != 0) {
    y_dp[0].insert(y_move[0]);
    y_dp[0].insert(-y_move[0]);
  }

  for (int i = 1; i < x_move.size(); i++) {
    for (auto itr_num : x_dp[i - 1]) {
      x_dp[i].insert(itr_num + x_move[i]);
      x_dp[i].insert(itr_num - x_move[i]);
    }
  }

  for (int i = 1; i < y_move.size(); i++) {
    for (auto itr_num : y_dp[i - 1]) {
      y_dp[i].insert(itr_num + y_move[i]);
      y_dp[i].insert(itr_num - y_move[i]);
    }
  }

  int goal_x, goal_y;

  cin >> goal_x >> goal_y;

  string ans = "No";

  if (x_dp[x_move.size() - 1].find(goal_x) != x_dp[x_move.size() - 1].end() &&
      y_dp[y_move.size() - 1].find(goal_y) != y_dp[y_move.size() - 1].end()) {
    ans = "Yes";
  }

  cout << ans << endl;

  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

vector<int> xa, ya;
string str;
int x, y, dpx[3000][16010], dpy[3000][16010];

int solvex(int pos, int amo) {
  if (pos >= xa.size()) {
    if (amo == x + 8000)
      return dpx[pos][amo] = 1;
    else
      return dpx[pos][amo] = 0;
  }
  if (dpx[pos][amo] != -1)
    return dpx[pos][amo];

  int ret = solvex(pos + 1, amo + xa[pos]) || solvex(pos + 1, amo - xa[pos]);

  return dpx[pos][amo] = ret;
}

int solvey(int pos, int amo) {
  if (dpy[pos][amo] != -1)
    return dpy[pos][amo];
  if (pos == ya.size()) {
    if (amo == y + 8000)
      return dpy[pos][amo] = 1;
    else
      return dpy[pos][amo] = 0;
  }

  int ret = solvey(pos + 1, amo + ya[pos]) || solvey(pos + 1, amo - ya[pos]);

  return dpy[pos][amo] = ret;
}

int main() {
  cin >> str;
  cin >> x >> y;
  int u = 0, v = 0;
  for (int i = 0; i < str.size(); i++) {
    u = 0;
    while (str[i] == 'F' && i < str.size())
      u++, i++;

    if (v == 0)
      xa.push_back(u);
    else
      ya.push_back(u);

    v = (v + 1) % 2;
    // i--;
    // printf("%d\n",u);
  }
  /*for(int i=0;i<xa.size();i++) printf("%d ",xa[i]);
  printf("\n");
  for(int i=0;i<ya.size();i++) printf("%d ",ya[i]);
  printf("\n");*/
  memset(dpx, -1, sizeof(dpx));
  memset(dpy, -1, sizeof(dpy));
  int rx = 0, ry = 0;
  if (xa.size() > 0)
    rx = solvex(1, xa[0] + 8000);
  ry = solvey(0, 8000);

  if (rx > 0 && ry > 0)
    printf("Yes\n");
  else
    printf("No\n");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

vector<int> xa, ya;
string str;
int x, y, dpx[4000][16010], dpy[4000][16010];

int solvex(int pos, int amo) {
  if (pos >= xa.size()) {
    if (amo == x + 8000)
      return dpx[pos][amo] = 1;
    else
      return dpx[pos][amo] = 0;
  }
  if (dpx[pos][amo] != -1)
    return dpx[pos][amo];

  int ret = solvex(pos + 1, amo + xa[pos]) || solvex(pos + 1, amo - xa[pos]);

  return dpx[pos][amo] = ret;
}

int solvey(int pos, int amo) {
  if (dpy[pos][amo] != -1)
    return dpy[pos][amo];
  if (pos == ya.size()) {
    if (amo == y + 8000)
      return dpy[pos][amo] = 1;
    else
      return dpy[pos][amo] = 0;
  }

  int ret = solvey(pos + 1, amo + ya[pos]) || solvey(pos + 1, amo - ya[pos]);

  return dpy[pos][amo] = ret;
}

int main() {
  cin >> str;
  cin >> x >> y;
  int u = 0, v = 0;
  for (int i = 0; i < str.size(); i++) {
    u = 0;
    while (str[i] == 'F' && i < str.size())
      u++, i++;

    if (v == 0)
      xa.push_back(u);
    else
      ya.push_back(u);

    v = (v + 1) % 2;
    // i--;
    // printf("%d\n",u);
  }
  /*for(int i=0;i<xa.size();i++) printf("%d ",xa[i]);
  printf("\n");
  for(int i=0;i<ya.size();i++) printf("%d ",ya[i]);
  printf("\n");*/
  memset(dpx, -1, sizeof(dpx));
  memset(dpy, -1, sizeof(dpy));
  int rx = 0, ry = 0;
  if (xa.size() > 0)
    rx = solvex(1, xa[0] + 8000);
  ry = solvey(0, 8000);

  if (rx > 0 && ry > 0)
    printf("Yes\n");
  else
    printf("No\n");
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  int x, y;
  cin >> S >> x >> y;
  S = S + 'T';
  int X[2010] = {};
  int Y[2010] = {};
  int count1 = 0, count2 = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) == 'F') {
      count1++;
    } else if (S.at(i) == 'T') {
      if (count2 % 2 == 0) {
        X[count2 / 2] = count1;
      } else if (count2 % 2 == 1) {
        Y[count2 / 2] = count1;
      }
      count2++;
      count1 = 0;
    }
  }
  bool dp1[16010][4010] = {};
  dp1[0][0] = 1;
  for (int j = 1; j <= count2 / 2; j++) {
    for (int i = 0; i <= 8000; i++) {
      dp1[i][j] = (dp1[i + X[j]][j - 1] | dp1[abs(i - X[j])][j - 1]);
    }
  }
  bool dp2[16010][4010] = {};
  dp2[0][0] = 1;
  for (int j = 1; j <= count2 / 2 + 1; j++) {
    for (int i = 0; i <= 8000; i++) {
      dp2[i][j] = (dp2[i + Y[j - 1]][j - 1] | dp2[abs(i - Y[j - 1])][j - 1]);
    }
  }
  if (dp1[abs(x - X[0])][count2 / 2] == 1 && dp2[abs(y)][count2 / 2 + 1] == 1) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  int x, y;
  cin >> S >> x >> y;
  S = S + 'T';
  int X[4010] = {};
  int Y[4010] = {};
  int count1 = 0, count2 = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) == 'F') {
      count1++;
    } else if (S.at(i) == 'T') {
      if (count2 % 2 == 0) {
        X[count2 / 2] = count1;
      } else if (count2 % 2 == 1) {
        Y[count2 / 2] = count1;
      }
      count2++;
      count1 = 0;
    }
  }
  bool dp1[16010][4010] = {};
  dp1[0][0] = 1;
  for (int j = 1; j <= count2 / 2; j++) {
    for (int i = 0; i <= 8000; i++) {
      dp1[i][j] = (dp1[i + X[j]][j - 1] | dp1[abs(i - X[j])][j - 1]);
    }
  }
  bool dp2[16010][4010] = {};
  dp2[0][0] = 1;
  for (int j = 1; j <= count2 / 2 + 1; j++) {
    for (int i = 0; i <= 8000; i++) {
      dp2[i][j] = (dp2[i + Y[j - 1]][j - 1] | dp2[abs(i - Y[j - 1])][j - 1]);
    }
  }
  if (dp1[abs(x - X[0])][count2 / 2] == 1 && dp2[abs(y)][count2 / 2 + 1] == 1) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}