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

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

bool check(vector<int> &v, int goal) {
  int size = v.size();
  int base = 10000;
  vector<vector<bool>> dp(size + 1, vector<bool>(base * 2, false));
  dp[0][base] = true;
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < base * 2; j++) {
      if (!dp[i][j])
        continue;
      dp[i + 1][j + v[i]] = true;
      dp[i + 1][j - v[i]] = true;
    }
  }
  return dp[size][goal + base];
}

int main(void) {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  int tmp = 0;
  vector<int> X, Y;
  bool isX = true;
  s += 'T';
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'T') {
      if (isX) {
        X.push_back(tmp);
      } else {
        Y.push_back(tmp);
      }
      isX = !isX;
      tmp = 0;
    } else {
      tmp++;
    }
  }
  if (X.size() >= 1) {
    x -= X[0];
    X.erase(X.begin());
  }
  cout << ((check(X, x) && check(Y, y)) ? "Yes" : "No") << endl;
  return 0;
}


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

bool check(vector<int> &v, int goal) {
  int size = v.size();
  int base = 20000;
  vector<vector<bool>> dp(size + 1, vector<bool>(base * 2, false));
  dp[0][base] = true;
  for (int i = 0; i < size; i++) {
    for (int j = 0; j < base * 2; j++) {
      if (!dp[i][j])
        continue;
      dp[i + 1][j + v[i]] = true;
      dp[i + 1][j - v[i]] = true;
    }
  }
  return dp[size][goal + base];
}

int main(void) {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  int tmp = 0;
  vector<int> X, Y;
  bool isX = true;
  s += 'T';
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'T') {
      if (isX) {
        X.push_back(tmp);
      } else {
        Y.push_back(tmp);
      }
      isX = !isX;
      tmp = 0;
    } else {
      tmp++;
    }
  }
  if (X.size() >= 1) {
    x -= X[0];
    X.erase(X.begin());
  }
  cout << ((check(X, x) && check(Y, y)) ? "Yes" : "No") << endl;
  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define all(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)

int main() {
  string s;
  cin >> s;
  s = s + 'T';
  int x, y;
  cin >> x >> y;
  vector<int> v1, v2;
  bool d = true;
  int cnt = 0;
  REP(i, s.size()) {
    if (s[i] == 'F')
      cnt++;
    if (s[i] == 'T') {
      if (d)
        v1.push_back(cnt);
      else
        v2.push_back(cnt);
      cnt = 0;
      d = !d;
    }
  }
  int m = v1.size(), n = v2.size();
  vector<vector<bool>> dp1(m, vector<bool>(16005)),
      dp2(n + 1, vector<bool>(16005));
  dp1[0][8000] = true;
  dp2[0][8000] = true;
  REP(i, m - 1) {
    REP(j, 16005) {
      if (dp1[i][j]) {
        dp1[i + 1][j + v1[i + 1]] = true;
        dp1[i + 1][j - v1[i + 1]] = true;
      }
    }
  }
  REP(i, n) {
    REP(j, 16005) {
      if (dp2[i][j]) {
        dp2[i + 1][j + v2[i]] = true;
        dp2[i + 1][j - v2[i]] = true;
      }
    }
  }
  bool f = (dp1[m - 1][8000 + x - v1[0]] && dp2[n][8000 + y]);
  cout << (f ? "Yes" : "No") << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
#define all(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)

int main() {
  string s;
  cin >> s;
  s = s + 'T';
  int x, y;
  cin >> x >> y;
  vector<int> v1, v2;
  bool d = true;
  int cnt = 0;
  REP(i, s.size()) {
    if (s[i] == 'F')
      cnt++;
    if (s[i] == 'T') {
      if (d)
        v1.push_back(cnt);
      else
        v2.push_back(cnt);
      cnt = 0;
      d = !d;
    }
  }
  int m = v1.size(), n = v2.size();
  vector<vector<bool>> dp1(m, vector<bool>(16005)),
      dp2(n + 1, vector<bool>(16005));
  dp1[0][8000] = true;
  dp2[0][8000] = true;
  REP(i, m - 1) {
    REP(j, 16005) {
      if (dp1[i][j]) {
        dp1[i + 1][j + v1[i + 1]] = true;
        dp1[i + 1][j - v1[i + 1]] = true;
      }
    }
  }
  REP(i, n) {
    REP(j, 16005) {
      if (dp2[i][j]) {
        dp2[i + 1][j + v2[i]] = true;
        dp2[i + 1][j - v2[i]] = true;
      }
    }
  }
  bool f = (8000 + x - v1[0] >= 0 && dp1[m - 1][8000 + x - v1[0]] &&
            dp2[n][8000 + y]);
  cout << (f ? "Yes" : "No") << endl;
  return 0;
}
Problem p03488 (C++)

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

typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2

using namespace std;

int MOD = 1000000007;
int xx[4010][16001] = {0};
int yy[4010][16001] = {0};

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

  int size = s.size();
  int i = 0;
  while (s[i] == 'F') {
    i++;
  }

  xx[0][i + 8000] = 1;
  yy[0][0 + 8000] = 1;
  int cnts = 1;
  int m = 1;
  for (; i < size;) {
    int cntt = 0;
    int cntf = 0;
    while (i < size && s[i] == 'T') {
      cntt++;
      i++;
    }
    while (i < size && s[i] == 'F') {
      cntf++;
      i++;
    }
    if (cntt % 2 == 1)
      m *= -1;
    if (m == 1) {
      rep(j, 16001) {
        if (xx[cnts - 1][j]) {
          xx[cnts][j + cntf] = 1;
          xx[cnts][j - cntf] = 1;
        }
        yy[cnts][j] = yy[cnts - 1][j];
      }
    } else {
      rep(j, 16001) {
        if (yy[cnts - 1][j]) {
          yy[cnts][j + cntf] = 1;
          yy[cnts][j - cntf] = 1;
        }
        xx[cnts][j] = xx[cnts - 1][j];
      }
    }
    cnts++;
  }

  if (xx[cnts - 1][x + 8000] && yy[cnts - 1][y + 8000]) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl

Fixed submission:
#include "bits/stdc++.h"

typedef long long ll;
// #define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2

using namespace std;

int MOD = 1000000007;
int xx[4010][16001] = {0};
int yy[4010][16001] = {0};

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

  int size = s.size();
  int i = 0;
  while (s[i] == 'F') {
    i++;
  }

  xx[0][i + 8000] = 1;
  yy[0][0 + 8000] = 1;
  int cnts = 1;
  int m = 1;
  for (; i < size;) {
    int cntt = 0;
    int cntf = 0;
    while (i < size && s[i] == 'T') {
      cntt++;
      i++;
    }
    while (i < size && s[i] == 'F') {
      cntf++;
      i++;
    }
    if (cntt % 2 == 1)
      m *= -1;
    if (m == 1) {
      rep(j, 16001) {
        if (xx[cnts - 1][j]) {
          xx[cnts][j + cntf] = 1;
          xx[cnts][j - cntf] = 1;
        }
        yy[cnts][j] = yy[cnts - 1][j];
      }
    } else {
      rep(j, 16001) {
        if (yy[cnts - 1][j]) {
          yy[cnts][j + cntf] = 1;
          yy[cnts][j - cntf] = 1;
        }
        xx[cnts][j] = xx[cnts - 1][j];
      }
    }
    cnts++;
  }

  if (xx[cnts - 1][x + 8000] && yy[cnts - 1][y + 8000]) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
std::string s;
std::vector<int> xs, ys;
bool dpx[8010][17000], dpy[8010][17000];
int x, y;
int main() {
  std::cin >> s;
  std::cin >> x >> y;
  int tmp = 0;
  bool xyt = true;
  for (int i = 0; i < (int)s.size(); i++) {
    if (s[i] == 'F')
      tmp++;
    else {
      if (xyt)
        xs.push_back(tmp);
      else
        ys.push_back(tmp);
      xyt = !xyt;
      tmp = 0;
    }
  }
  if (xyt)
    xs.push_back(tmp);
  else
    ys.push_back(tmp);
  dpx[0][xs[0] + 8500] = true;
  for (int i = 1; i < (int)xs.size(); i++) {
    for (int j = 0; j < 17000; j++) {
      if (j - xs[i] >= 0) {
        dpx[i][j] = dpx[i - 1][j - xs[i]];
      }
      if (!dpx[i][j]) {
        if (j + xs[i] < 17000) {
          dpx[i][j] = dpx[i - 1][j + xs[i]];
        }
      }
    }
  }
  dpy[0][8500 - ys[0]] = true;
  dpy[0][8500 + ys[0]] = true;
  for (int i = 1; i < (int)ys.size(); i++) {
    for (int j = 0; j < 17000; j++) {
      if (j - ys[i] >= 0) {
        dpy[i][j] = dpy[i - 1][j - ys[i]];
      }
      if (!dpy[i][j]) {
        if (j + ys[i] < 17000) {
          dpy[i][j] = dpy[i - 1][j + ys[i]];
        }
      }
    }
  }
  if (dpy[ys.size() - 1][y + 8500] && dpx[xs.size() - 1][x + 8500]) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
std::string s;
std::vector<int> xs, ys;
bool dpx[8010][17000], dpy[8010][17000];
int x, y;
int main() {
  std::cin >> s;
  std::cin >> x >> y;
  int tmp = 0;
  bool xyt = true;
  for (int i = 0; i < (int)s.size(); i++) {
    if (s[i] == 'F')
      tmp++;
    else {
      if (xyt)
        xs.push_back(tmp);
      else
        ys.push_back(tmp);
      xyt = !xyt;
      tmp = 0;
    }
  }
  if (xyt)
    xs.push_back(tmp);
  else
    ys.push_back(tmp);
  xs.push_back(0);
  ys.push_back(0);
  dpx[0][xs[0] + 8500] = true;
  for (int i = 1; i < (int)xs.size(); i++) {
    for (int j = 0; j < 17000; j++) {
      if (j - xs[i] >= 0) {
        dpx[i][j] = dpx[i - 1][j - xs[i]];
      }
      if (!dpx[i][j]) {
        if (j + xs[i] < 17000) {
          dpx[i][j] = dpx[i - 1][j + xs[i]];
        }
      }
    }
  }
  dpy[0][8500 - ys[0]] = true;
  dpy[0][8500 + ys[0]] = true;
  for (int i = 1; i < (int)ys.size(); i++) {
    for (int j = 0; j < 17000; j++) {
      if (j - ys[i] >= 0) {
        dpy[i][j] = dpy[i - 1][j - ys[i]];
      }
      if (!dpy[i][j]) {
        if (j + ys[i] < 17000) {
          dpy[i][j] = dpy[i - 1][j + ys[i]];
        }
      }
    }
  }
  if (dpy[ys.size() - 1][y + 8500] && dpx[xs.size() - 1][x + 8500]) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
const int MAX_L = 16016;
const int MAX_SZ = 2020;

int main() {
  string S;
  int x, y;
  cin >> S >> x >> y;
  vector<int> M;
  int cnt = 0;
  S = S + 'T';
  for (int i = 0; i < (int)S.length(); i++) {
    if (S[i] == 'F')
      cnt++;
    else {
      M.emplace_back(cnt);
      cnt = 0;
    }
  }

  vector<int> X, Y;
  for (int i = 1; i < (int)M.size(); i++) {
    if (i & 1)
      Y.emplace_back(M[i]);
    else
      X.emplace_back(M[i]);
  }

  int lx = X.size(), ly = Y.size();
  const int geta = 8008;

  bool dp[MAX_SZ][MAX_L] = {0};
  dp[0][M[0] + geta] = true;
  for (int i = 0; i < lx; i++) {
    for (int j = 0; j < MAX_L; j++)
      if (dp[i][j]) {
        if (j + X[i] < MAX_L)
          dp[i + 1][j + X[i]] = true;
        if (0 <= j - X[i])
          dp[i + 1][j - X[i]] = true;
      }
  }

  bool dpp[MAX_SZ][MAX_L] = {0};
  dpp[0][geta] = true;
  for (int i = 0; i < ly; i++) {
    for (int j = 0; j < MAX_L; j++)
      if (dpp[i][j]) {
        if (j + Y[i] < MAX_L)
          dpp[i + 1][j + Y[i]] = true;
        if (0 <= j - Y[i])
          dpp[i + 1][j - Y[i]] = true;
      }
  }

  cout << ((dp[lx][geta + x] && dpp[ly][geta + y]) ? "Yes" : "No") << endl;
  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
const int MAX_L = 16016;
const int MAX_SZ = 4000;

int main() {
  string S;
  int x, y;
  cin >> S >> x >> y;
  vector<int> M;
  int cnt = 0;
  S = S + 'T';
  for (int i = 0; i < (int)S.length(); i++) {
    if (S[i] == 'F')
      cnt++;
    else {
      M.emplace_back(cnt);
      cnt = 0;
    }
  }

  vector<int> X, Y;
  for (int i = 1; i < (int)M.size(); i++) {
    if (i & 1)
      Y.emplace_back(M[i]);
    else
      X.emplace_back(M[i]);
  }

  int lx = X.size(), ly = Y.size();
  const int geta = 8008;

  bool dp[MAX_SZ][MAX_L] = {0};
  dp[0][M[0] + geta] = true;
  for (int i = 0; i < lx; i++) {
    for (int j = 0; j < MAX_L; j++)
      if (dp[i][j]) {
        if (j + X[i] < MAX_L)
          dp[i + 1][j + X[i]] = true;
        if (0 <= j - X[i])
          dp[i + 1][j - X[i]] = true;
      }
  }

  bool dpp[MAX_SZ][MAX_L] = {0};
  dpp[0][geta] = true;
  for (int i = 0; i < ly; i++) {
    for (int j = 0; j < MAX_L; j++)
      if (dpp[i][j]) {
        if (j + Y[i] < MAX_L)
          dpp[i + 1][j + Y[i]] = true;
        if (0 <= j - Y[i])
          dpp[i + 1][j - Y[i]] = true;
      }
  }

  cout << ((dp[lx][geta + x] && dpp[ly][geta + y]) ? "Yes" : "No") << endl;
  return 0;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define for0(i, n) for (int i = 0; i < (n); i++)
using namespace std;
string s;
const int k = 8100;
int cnt, dpx[2500][2 * k + 10], x, dpy[2500][2 * k + 10], y, mode = 2;
int get() {
  int r;
  cin >> r;
  return r;
}
signed main() {
  cin >> s;
  s += 'T';
  dpy[y++][k] = 1;
  for (char c : s) {
    if (c == 'F')
      cnt++;
    else {
      if (mode == 2)
        dpx[x++][cnt + k] = 1;
      else if (mode == 1 && cnt > 0) {
        for0(i, 2 * k) if (dpy[y - 1][i]) {
          if (i + cnt < 2 * k)
            dpy[y][i + cnt] = 1;
          if (i - cnt >= 0)
            dpy[y][i - cnt] = 1;
        }
        y++;
      } else if (mode == 0 && cnt > 0) {
        for0(i, 2 * k) if (dpx[x - 1][i]) {
          if (i + cnt < 2 * k)
            dpx[x][i + cnt] = 1;
          if (i - cnt >= 0)
            dpx[x][i - cnt] = 1;
        }
        x++;
      }
      cnt = 0;
      mode = (++mode) % 2;
    }
  }
  cout << (dpx[x - 1][get() + k] && dpy[y - 1][get() + k] ? "Yes\n" : "No\n");
}

Fixed submission:
#include <bits/stdc++.h>
#define for0(i, n) for (int i = 0; i < (n); i++)
using namespace std;
string s;
const int k = 8100;
int cnt, dpx[2670][2 * k + 10], x, dpy[2670][2 * k + 10], y, mode = 2;
int get() {
  int r;
  cin >> r;
  return r;
}
signed main() {
  cin >> s;
  s += 'T';
  dpy[y++][k] = 1;
  for (char c : s) {
    if (c == 'F')
      cnt++;
    else {
      if (mode == 2)
        dpx[x++][cnt + k] = 1;
      else if (mode == 1 && cnt > 0) {
        for0(i, 2 * k) if (dpy[y - 1][i]) {
          if (i + cnt < 2 * k)
            dpy[y][i + cnt] = 1;
          if (i - cnt >= 0)
            dpy[y][i - cnt] = 1;
        }
        y++;
      } else if (mode == 0 && cnt > 0) {
        for0(i, 2 * k) if (dpx[x - 1][i]) {
          if (i + cnt < 2 * k)
            dpx[x][i + cnt] = 1;
          if (i - cnt >= 0)
            dpx[x][i - cnt] = 1;
        }
        x++;
      }
      cnt = 0;
      mode = (++mode) % 2;
    }
  }
  cout << (dpx[x - 1][get() + k] && dpy[y - 1][get() + k] ? "Yes\n" : "No\n");
}
Problem p03488 (C++)

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

#define MAX 8005

int convert_idx(int x) { return x + MAX; }

bool is_valid(vector<int> c, int t) {
  vector<vector<bool>> dp(c.size() + 1, vector<bool>(2 * MAX, false));
  dp.front()[convert_idx(0)] = true;
  for (int i = 0; i < c.size(); i++) {
    for (int j = 0; j < dp[i].size(); j++) {
      if (dp[i][j]) {
        dp[i + 1][j + c[i]] = true;
        dp[i + 1][j - c[i]] = true;
      }
    }
  }
  return dp.back()[convert_idx(t)];
}

int main() {
  string S;
  cin >> S;

  int x, y;
  cin >> x >> y;

  auto itr = S.begin();
  while (itr != S.end() && *itr == 'F') {
    x--;
    itr++;
  }

  vector<vector<int>> count(2, vector<int>(1, 0));
  int direction = 0;
  for (; itr != S.end(); itr++) {
    if (*itr == 'T') {
      direction ^= 1;
      count[direction].push_back(0);
    }
    if (*itr == 'F') {
      count[direction].back()++;
    }
  }

  if (is_valid(count[0], x) && is_valid(count[1], y)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}


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

#define MAX 8005

int convert_idx(int x) { return x + MAX; }

bool is_valid(vector<int> c, int t) {
  vector<vector<bool>> dp(c.size() + 1, vector<bool>(2 * MAX, false));
  dp.front()[convert_idx(0)] = true;
  for (int i = 0; i < c.size(); i++) {
    for (int j = 0; j < dp[i].size(); j++) {
      if (dp[i][j]) {
        dp[i + 1][j + c[i]] = true;
        dp[i + 1][j - c[i]] = true;
      }
    }
  }
  if (0 <= convert_idx(t) && convert_idx(t) < dp.back().size()) {
    return dp.back()[convert_idx(t)];
  } else {
    return false;
  }
}

int main() {
  string S;
  cin >> S;

  int x, y;
  cin >> x >> y;

  auto itr = S.begin();
  while (itr != S.end() && *itr == 'F') {
    x--;
    itr++;
  }

  vector<vector<int>> count(2, vector<int>(1, 0));
  int direction = 0;
  for (; itr != S.end(); itr++) {
    if (*itr == 'T') {
      direction ^= 1;
      count[direction].push_back(0);
    }
    if (*itr == 'F') {
      count[direction].back()++;
    }
  }

  if (is_valid(count[0], x) && is_valid(count[1], y)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define INF 1e9
#define ll long long
#define MAXS 8005
#define vi vector<int>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

string S;
int X, Y;
vi vert, hori;
bool dp[2000][8005];

bool check(int goal, vi vec) {
  int n = vec.size();

  rep(i, n + 1) { rep(j, MAXS) dp[i][j] = 0; }

  dp[0][0] = 1;

  for (int i = 1; i <= n; i++) {
    int val = vec[i - 1];

    rep(j, MAXS) {
      if (dp[i - 1][j] == 1) {
        if (j - val >= 0)
          dp[i][j - val] = 1;
        if (j + val < MAXS)
          dp[i][j + val] = 1;
      }
    }
  }

  return dp[n][goal];
}

int main() {
  cin >> S >> X >> Y;
  if (S[S.size() - 1] != 'T')
    S += 'T'; // パディング

  int tmp = 0;
  bool state = true;

  rep(i, S.size()) {
    if (S[i] == 'F') {
      tmp++;
    } else {
      if (state)
        hori.push_back(tmp);
      else
        vert.push_back(tmp);
      tmp = 0;
      state = !state;
    }
  }

  X -= hori[0];
  hori.erase(hori.begin()); // 初期値だけシフトして対称性を保つ
  X = abs(X);
  Y = abs(Y);
  // printf("goal: (%d,%d)\n",X,Y);

  // printf("vertical: ");
  // rep(i,vert.size())
  //     cout << vert[i] << " ";
  // cout << endl;

  // printf("horizontal: ");
  // rep(i,hori.size())
  //     cout << hori[i] << " ";
  // cout << endl;

  if (check(Y, vert) && check(X, hori))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define INF 1e9
#define ll long long
#define MAXS 8005
#define vi vector<int>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

string S;
int X, Y;
vi vert, hori;
bool dp[MAXS][MAXS];

bool check(int goal, vi vec) {
  int n = vec.size();

  rep(i, n + 1) { rep(j, MAXS) dp[i][j] = 0; }

  dp[0][0] = 1;

  for (int i = 1; i <= n; i++) {
    int val = vec[i - 1];

    rep(j, MAXS) {
      if (dp[i - 1][j] == 1) {
        if (j - val >= 0)
          dp[i][j - val] = 1;
        if (j + val < MAXS)
          dp[i][j + val] = 1;
      }
    }
  }

  return dp[n][goal];
}

int main() {
  cin >> S >> X >> Y;
  if (S[S.size() - 1] != 'T')
    S += 'T'; // パディング

  int tmp = 0;
  bool state = true;

  rep(i, S.size()) {
    if (S[i] == 'F') {
      tmp++;
    } else {
      if (state)
        hori.push_back(tmp);
      else
        vert.push_back(tmp);
      tmp = 0;
      state = !state;
    }
  }

  X -= hori[0];
  hori.erase(hori.begin()); // 初期値だけシフトして対称性を保つ
  X = abs(X);
  Y = abs(Y);
  // printf("goal: (%d,%d)\n",X,Y);

  // printf("vertical: ");
  // rep(i,vert.size())
  //     cout << vert[i] << " ";
  // cout << endl;

  // printf("horizontal: ");
  // rep(i,hori.size())
  //     cout << hori[i] << " ";
  // cout << endl;

  if (check(Y, vert) && check(X, hori))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03488 (C++)

Buggy submission (status: Memory Limit Exceeded):
#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>(20000));
  vector<vector<int>> ep(M + 1, vector<int>(20000));
  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;
}


Fixed submission:
#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;
}
Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int MAX_N = 8e3 + 10, offset = 8e3;

std::bitset<MAX_N << 1> f[MAX_N];

char op[MAX_N];
int len, init[2], cnt[2], to[2], num[2][MAX_N];

bool work(int ty) {
  for (int i = 0; i <= cnt[ty]; ++i)
    f[i].reset();

  f[0][init[ty] + offset] = 1;
  for (int i = 1; i <= cnt[ty]; ++i)
    f[i] = (f[i - 1] >> num[ty][i]) | (f[i - 1] << num[ty][i]);

  return f[cnt[ty]][to[ty] + offset] == 1;
}

int main() {
  scanf("%s", op);
  len = strlen(op);
  scanf("%d %d", &to[0], &to[1]);

  ///

  cnt[0] = cnt[1] = 0;
  init[0] = init[1] = 0;
  int i = 0;
  for (; op[i] != 'T'; ++i, ++init[0])
    ;

  int ty = 1;
  for (++i; i < len; ty ^= 1) {
    int p = i;
    for (; p < len && op[p] != 'T'; ++p)
      ;

    if (p > i)
      num[ty][++cnt[ty]] = p - i;
    i = p + 1;
  }

  bool ans = work(0) & work(1);
  if (ans)
    printf("Yes\n");
  else
    printf("No\n");

  return 0;
}

Fixed submission:
#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int MAX_N = 8e3 + 10, offset = 8e3;

std::bitset<MAX_N << 1> f[MAX_N];

char op[MAX_N];
int len, init[2], cnt[2], to[2], num[2][MAX_N];

bool work(int ty) {
  for (int i = 0; i <= cnt[ty]; ++i)
    f[i].reset();

  f[0][init[ty] + offset] = 1;
  for (int i = 1; i <= cnt[ty]; ++i)
    f[i] = (f[i - 1] >> num[ty][i]) | (f[i - 1] << num[ty][i]);

  return f[cnt[ty]][to[ty] + offset] == 1;
}

int main() {
  scanf("%s", op);
  len = strlen(op);
  scanf("%d %d", &to[0], &to[1]);

  ///

  cnt[0] = cnt[1] = 0;
  init[0] = init[1] = 0;
  int i = 0;
  for (; i < len && op[i] != 'T'; ++i, ++init[0])
    ;

  int ty = 1;
  for (++i; i < len; ty ^= 1) {
    int p = i;
    for (; p < len && op[p] != 'T'; ++p)
      ;

    if (p > i)
      num[ty][++cnt[ty]] = p - i;
    i = p + 1;
  }

  bool ans = work(0) & work(1);
  if (ans)
    printf("Yes\n");
  else
    printf("No\n");

  return 0;
}
Problem p03489 (C++)

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

using namespace std;

static const int INF = 1e9 + 7;

void hawawa() {
  int n;
  cin >> n;
  int ans = n;
  vector<int> a(n);
  for (auto &&i : a) {
    cin >> i;
  }
  sort(a.begin(), a.end());
  auto b = a;
  b.erase(unique(b.begin(), b.end()), b.end());
  for (int i = 0; i < b.size(); i++) {
    int cnt = count(a.begin(), a.end(), b[i]);
    if (cnt >= b[i]) {
      ans -= b[i];
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  hawawa();
}

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

using namespace std;

static const int INF = 1e9 + 7;

void hawawa() {
  int n;
  cin >> n;
  int ans = n;
  vector<int> a(n);
  for (auto &&i : a) {
    cin >> i;
  }
  sort(a.begin(), a.end());
  auto b = a;
  b.erase(unique(b.begin(), b.end()), b.end());
  for (int i = 0; i < b.size(); i++) {
    int cnt = upper_bound(a.begin(), a.end(), b[i]) -
              lower_bound(a.begin(), a.end(), b[i]);
    if (cnt >= b[i]) {
      ans -= b[i];
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  hawawa();
}
Problem p03489 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define debug(x) cerr << #x << '=' << x << '\n'

int a[1234567];

void nxt(int &j) {
  int k = a[j];
  while (a[j] == k)
    j++;
}
int num(int j) {
  int k = j;
  while (a[j + 1] == a[k])
    j++;
  //	debug(j);
  return j - k + 1;
}

int main(void) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  sort(a + 1, a + 1 + n);
  int ans = 0;
  int i = 1;
  while (i <= n) {
    int k = num(i);
    if (k >= a[i]) {
      ans += k - a[i];
      nxt(i);
    } else {
      if (k < a[i]) {
        ans += k;
      }
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define debug(x) cerr << #x << '=' << x << '\n'

int a[1234567];

void nxt(int &j) {
  int k = a[j];
  while (a[j] == k)
    j++;
}
int num(int j) {
  int k = j;
  while (a[j + 1] == a[k])
    j++;
  //	debug(j);
  return j - k + 1;
}

int main(void) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  sort(a + 1, a + 1 + n);
  int ans = 0;
  int i = 1;
  while (i <= n) {
    int k = num(i);
    if (k >= a[i]) {
      ans += k - a[i];
      nxt(i);
    } else {
      if (k < a[i]) {
        ans += k;
        nxt(i);
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p03489 (C++)

Buggy submission (status: Runtime Error):
#define REP(i, s, e) for (register int i = s; i <= e; i++)
#define DREP(i, s, e) for (register int i = s; i >= e; i--)

#define DEBUG                                                                  \
  fprintf(stderr, "Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__)
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

#include <bits/stdc++.h>

using namespace std;
const int maxn = 10010;

template <typename T> T read() {
  T ans(0), p(1);
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    ans = ans * 10 + c - 48;
    c = getchar();
  }
  return ans * p;
}

int m, n, k, p[maxn];
string a[maxn];

inline int to(string x) {
  register int len = x.size(), ans(0);

  REP(i, 0, len - 1) ans = ans * 10 + x[i] - 48;
  return ans;
}

map<string, int> Sum, vis;

int main() {
#ifdef CraZYali
  freopen("c.in", "r", stdin);
  freopen("c.out", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin >> n;
  REP(i, 1, n) cin >> a[i], Sum[a[i]]++, p[i] = to(a[i]);
  REP(i, 1, n)
  if (!vis[a[i]]) {
    vis[a[i]] = 1;
    if (Sum[a[i]] > p[i])
      k += Sum[a[i]] - p[i];
    else if (Sum[a[i]] < p[i])
      k += Sum[a[i]];
  }
  cout << k;
  return 0;
}

Fixed submission:
#define REP(i, s, e) for (register int i = s; i <= e; i++)
#define DREP(i, s, e) for (register int i = s; i >= e; i--)

#define DEBUG                                                                  \
  fprintf(stderr, "Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__)
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

#include <bits/stdc++.h>

using namespace std;
const int maxn = 100010;

template <typename T> T read() {
  T ans(0), p(1);
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    ans = ans * 10 + c - 48;
    c = getchar();
  }
  return ans * p;
}

int m, n, k, p[maxn];
string a[maxn];

inline int to(string x) {
  register int len = x.size(), ans(0);

  REP(i, 0, len - 1) ans = ans * 10 + x[i] - 48;
  return ans;
}

map<string, int> Sum, vis;

int main() {
#ifdef CraZYali
  freopen("c.in", "r", stdin);
  freopen("c.out", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin >> n;
  REP(i, 1, n) cin >> a[i], Sum[a[i]]++, p[i] = to(a[i]);
  REP(i, 1, n)
  if (!vis[a[i]]) {
    vis[a[i]] = 1;
    if (Sum[a[i]] > p[i])
      k += Sum[a[i]] - p[i];
    else if (Sum[a[i]] < p[i])
      k += Sum[a[i]];
  }
  cout << k;
  return 0;
}
Problem p03489 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, s, n) for (int i = (s); i < (n); ++i)
#define MAX 10000
typedef long long LL;
typedef unsigned long long ULL;
int check[MAX];
int cmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; }
int main(void) {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &check[i]);
  qsort(check, n, 4, cmp);
  int ret = 0;
  int cur = check[0], num = 1;
  rep1(i, 1, n) {
    if (check[i] != cur) {
      if (num > cur)
        ret += num - cur;
      else if (cur > num)
        ret += num;
      cur = check[i];
      num = 1;
    } else
      num++;
  }
  if (num > cur)
    ret += num - cur;
  else if (cur > num)
    ret += num;
  printf("%d", ret);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, s, n) for (int i = (s); i < (n); ++i)
#define MAX 100000
typedef long long LL;
typedef unsigned long long ULL;
int check[MAX];
int cmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; }
int main(void) {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &check[i]);
  qsort(check, n, 4, cmp);
  int ret = 0;
  int cur = check[0], num = 1;
  rep1(i, 1, n) {
    if (check[i] != cur) {
      if (num > cur)
        ret += num - cur;
      else if (cur > num)
        ret += num;
      cur = check[i];
      num = 1;
    } else
      num++;
  }
  if (num > cur)
    ret += num - cur;
  else if (cur > num)
    ret += num;
  printf("%d", ret);
  return 0;
}
Problem p03489 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <list>
#include <math.h>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

int main() {
  int N;
  int sum = 0;
  unordered_map<int, int> count;

  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
    count[a.at(i)] = 0;
  }
  for (int j = 0; j < N; j++)
    count[a.at(j)]++;
  for (auto it = count.begin(); it != count.end(); it++)
    sum +=
        (*it).second >= (*it).first ? (*it).second - (*it).first : (*it).second;
  return sum;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <list>
#include <math.h>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

int main() {
  int N;
  int sum = 0;
  unordered_map<int, int> count;

  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
    count[a.at(i)] = 0;
  }
  for (int j = 0; j < N; j++)
    count[a.at(j)]++;
  for (auto it = count.begin(); it != count.end(); it++)
    sum +=
        (*it).second >= (*it).first ? (*it).second - (*it).first : (*it).second;
  cout << sum;
  return 0;
}
Problem p03489 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int s[100010];
int cmp[100010];
int cnt[100010];
int n, tot, ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s[i]);
    bool f = 0;
    for (int j = 1; j <= tot; j++) {
      if (cmp[j] == s[i]) {
        cnt[j]--;
        f = 1;
      }
    }
    if (!f)
      cmp[++tot] = s[i], cnt[tot] = s[i] - 1;
  }
  for (int i = 1; i <= tot; i++) {
    if (!cnt[i])
      continue;
    if (abs(cnt[i]) != cnt[i]) {
      ans -= cnt[i];
    } else
      ans += cmp[i] - cnt[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int s[100010];
int cmp[100010];
int cnt[100010];
int n, tot, ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s[i]);
    if (s[i] > 100000) {
      ans++;
      continue;
    }
    bool f = 0;
    for (int j = 1; j <= tot; j++) {
      if (cmp[j] == s[i]) {
        cnt[j]--;
        f = 1;
      }
    }
    if (!f)
      cmp[++tot] = s[i], cnt[tot] = s[i] - 1;
  }
  for (int i = 1; i <= tot; i++) {
    if (!cnt[i])
      continue;
    if (abs(cnt[i]) != cnt[i]) {
      ans -= cnt[i];
    } else
      ans += cmp[i] - cnt[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03489 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
template <typename __T> inline void read(__T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
int cnt[10005];
int n;
int main() {
  read(n);
  int x;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    read(x);
    if (x <= n)
      cnt[x]++;
    else
      ans++;
  }
  for (int i = 1; i <= n; i++)
    ans += (cnt[i] >= i ? cnt[i] - i : cnt[i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
template <typename __T> inline void read(__T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
int cnt[100005];
int n;
int main() {
  read(n);
  int x;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    read(x);
    if (x <= n)
      cnt[x]++;
    else
      ans++;
  }
  for (int i = 1; i <= n; i++)
    ans += (cnt[i] >= i ? cnt[i] - i : cnt[i]);
  cout << ans << endl;
  return 0;
}
Problem p03489 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    m[x]++;
  }
  ll ans = 0;
  for (auto k : m) {
    if (k.se > 0) {
      if (k.se < k.fi)
        ans += k.se;
      else if (k.se >= k.fi)
        ans += abs(k.se - k.fi);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    m[x]++;
  }
  ll ans = 0;
  for (auto k : m) {
    if (k.se > 0) {
      if (k.se < k.fi)
        ans += k.se;
      else if (k.se >= k.fi)
        ans += abs(k.se - k.fi);
    }
  }
  cout << ans << endl;
}
Problem p03489 (C++)

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

int b[10050], ans, n, cnt;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  sort(b + 1, b + n + 1);
  cnt = 1;
  for (int i = 2; i <= n; i++)
    if (b[i] != b[i - 1]) {
      if (cnt < b[i - 1])
        ans += cnt;
      else if (cnt > b[i - 1])
        ans += cnt - b[i - 1];
      cnt = 1;
    } else
      cnt++;
  if (cnt < b[n])
    ans += cnt;
  else if (cnt > b[n])
    ans += cnt - b[n];
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int b[100050], ans, n, cnt;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  sort(b + 1, b + n + 1);
  cnt = 1;
  for (int i = 2; i <= n; i++)
    if (b[i] != b[i - 1]) {
      if (cnt < b[i - 1])
        ans += cnt;
      else if (cnt > b[i - 1])
        ans += cnt - b[i - 1];
      cnt = 1;
    } else
      cnt++;
  if (cnt < b[n])
    ans += cnt;
  else if (cnt > b[n])
    ans += cnt - b[n];
  printf("%d\n", ans);
  return 0;
}
Problem p03490 (C++)

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

const int N = 100000;

bool dp[2][N + N], tmp[N + N];

void modify(int dir, int cnt) {
  memset(tmp, 0, sizeof(tmp));
  for (int i = 0; i < N + N; i++) {
    if (dp[dir][i]) {
      tmp[i - cnt] = 1;
      tmp[i + cnt] = 1;
    }
  }
  for (int i = 0; i < N + N; i++)
    dp[dir][i] = tmp[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  int x, y;
  cin >> s >> x >> y;
  s += 'T';
  int n = s.length(), dir = 0; // x-axis
  dp[0][N] = dp[1][N] = 1;
  bool start = 1;
  for (int i = 0, cnt = 0; i < n; i++) {
    if (start && s[i] == 'F') {
      dp[0][N + i + 1] = 1;
      dp[0][N + i] = 0;
      continue;
    } else
      start = 0;
    if (s[i] == 'T') {
      modify(dir, cnt);
      cnt = 0;
      dir ^= 1;
    } else {
      cnt++;
    }
  }
  if (dp[0][N + x] && dp[1][N + y])
    cout << "Yes\n";
  else
    cout << "No\n";
  return 0;
}


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

const int N = 10000;

bool dp[2][N + N], tmp[N + N];

void modify(int dir, int cnt) {
  memset(tmp, 0, sizeof(tmp));
  for (int i = 0; i < N + N; i++) {
    if (dp[dir][i]) {
      tmp[i - cnt] = 1;
      tmp[i + cnt] = 1;
    }
  }
  for (int i = 0; i < N + N; i++)
    dp[dir][i] = tmp[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  int x, y;
  cin >> s >> x >> y;
  s += 'T';
  int n = s.length(), dir = 0; // x-axis
  dp[0][N] = dp[1][N] = 1;
  bool start = 1;
  for (int i = 0, cnt = 0; i < n; i++) {
    if (start && s[i] == 'F') {
      dp[0][N + i + 1] = 1;
      dp[0][N + i] = 0;
      continue;
    } else
      start = 0;
    if (s[i] == 'T') {
      modify(dir, cnt);
      cnt = 0;
      dir ^= 1;
    } else {
      cnt++;
    }
  }
  if (dp[0][N + x] && dp[1][N + y])
    cout << "Yes\n";
  else
    cout << "No\n";
  return 0;
}
Problem p03490 (C++)

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

char s[8008];
int ex, ey;
bool dp[2][16006];
vector<int> dv[2];

bool foo(int dist, vector<int> &mv) {
  memset(dp, 0, sizeof(dp));
  int f = 0;
  dp[0][8000] = true;
  for (int x : mv) {
    for (int i = 0; i <= 16000; i++) {
      if (!dp[f][i])
        continue;
      dp[!f][i + x] = dp[!f][i - x] = true;
    }
    memset(dp[f], 0, sizeof(dp[f]));
    f = !f;
  }
  return dp[f][dist];
}

int main() {
  scanf("%s%d%d", s, &ex, &ey);
  ex += 8000;
  ey += 8000;
  int i = 0;
  while (s[i] == 'F') {
    i++;
    ex--;
  }
  int d = 0, dir = 0;
  for (; s[i]; i++) {
    if (s[i] == 'T') {
      if (d)
        dv[dir].push_back(d);
      dir = !dir;
      d = 0;
    } else {
      d++;
    }
  }
  if (d)
    dv[dir].push_back(d);
  puts((foo(ex, dv[0]) && foo(ey, dv[1])) ? "Yes" : "No");
}


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

char s[8008];
int ex, ey;
bool dp[2][16006];
vector<int> dv[2];

bool foo(int dist, vector<int> &mv) {
  memset(dp, 0, sizeof(dp));
  int f = 0;
  dp[0][8000] = true;
  for (int x : mv) {
    for (int i = 0; i <= 16000; i++) {
      if (!dp[f][i])
        continue;
      dp[!f][i + x] = dp[!f][i - x] = true;
    }
    memset(dp[f], 0, sizeof(dp[f]));
    f = !f;
  }
  return dp[f][dist];
}

int main() {
  scanf("%s%d%d", s, &ex, &ey);
  ex += 8000;
  ey += 8000;
  int i = 0;
  while (s[i] == 'F') {
    i++;
    ex--;
  }
  if (ex < 0) {
    puts("No");
    return 0;
  }
  int d = 0, dir = 0;
  for (; s[i]; i++) {
    if (s[i] == 'T') {
      if (d)
        dv[dir].push_back(d);
      dir = !dir;
      d = 0;
    } else {
      d++;
    }
  }
  if (d)
    dv[dir].push_back(d);
  puts((foo(ex, dv[0]) && foo(ey, dv[1])) ? "Yes" : "No");
}
Problem p03490 (C++)

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

using namespace std;

typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;

string s;
lli x, y;

vll mvx;
vll mvy;

vector<string> split(string::iterator begin, string::iterator end, char sep) {
  vector<string> ret;
  for (auto itr = begin;; itr++) {
    auto jtr = itr;
    while (jtr != end && *jtr != sep)
      jtr++;
    // cout << string(itr,jtr) << endl;
    ret.push_back(string(itr, jtr));
    itr = jtr;
    if (itr == end)
      break;
  }
  return ret;
}

bool solve(vll &x, lli t) {
  lli s = accumulate(x.begin(), x.end(), 0) + 10;
  mat dp = mat(x.size() + 1, vll(s * 2 + 1, 0));
  dp[0][s] = 1;
  for (lli i = 0; i < x.size(); i++) {
    for (lli j = 0; j < s * 2; j++) {
      if (dp[i][j] == 1)
        dp[i + 1][j - x[i]] = dp[i + 1][j + x[i]] = 1;
    }
  }
  if (dp[x.size()][s + t] == 1)
    return true;
  else
    return false;
}

int main() {
  cin >> s;
  cin >> x >> y;
  vector<string> order = split(s.begin(), s.end(), 'T');
  x -= order[0].size();
  for (lli i = 1; i < order.size(); i++) {
    if (i % 2 == 0)
      mvx.push_back(order[i].size());
    else
      mvy.push_back(order[i].size());
  }
  // for(lli i = 0;i < mvx.size();i++) cout << mvx[i] << endl;
  // for(lli i = 0;i < mvy.size();i++) cout << mvy[i] << endl;
  if (solve(mvx, x) && solve(mvy, y)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


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

using namespace std;

typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;

string s;
lli x, y;

vll mvx;
vll mvy;

vector<string> split(string::iterator begin, string::iterator end, char sep) {
  vector<string> ret;
  for (auto itr = begin;; itr++) {
    auto jtr = itr;
    while (jtr != end && *jtr != sep)
      jtr++;
    // cout << string(itr,jtr) << endl;
    ret.push_back(string(itr, jtr));
    itr = jtr;
    if (itr == end)
      break;
  }
  return ret;
}

bool solve(vll &x, lli t) {
  lli s = accumulate(x.begin(), x.end(), 0) + 10;
  mat dp = mat(x.size() + 1, vll(s * 2 + 1, 0));
  dp[0][s] = 1;
  for (lli i = 0; i < x.size(); i++) {
    for (lli j = 0; j < s * 2; j++) {
      if (dp[i][j] == 1)
        dp[i + 1][j - x[i]] = dp[i + 1][j + x[i]] = 1;
    }
  }
  if (s + t < dp[x.size()].size() && dp[x.size()][s + t] == 1)
    return true;
  else
    return false;
}

int main() {
  cin >> s;
  cin >> x >> y;
  vector<string> order = split(s.begin(), s.end(), 'T');
  x -= order[0].size();
  for (lli i = 1; i < order.size(); i++) {
    if (i % 2 == 0)
      mvx.push_back(order[i].size());
    else
      mvy.push_back(order[i].size());
  }
  // for(lli i = 0;i < mvx.size();i++) cout << mvx[i] << endl;
  // for(lli i = 0;i < mvy.size();i++) cout << mvy[i] << endl;
  if (solve(mvx, x) && solve(mvy, y)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define N 20011
#define delta N / 2
using namespace std;
char s[8001] = {};
int n, tx, ty, m;
int a[8001];
int a1[8001] = {}, a2[8001] = {};
int now, pre;
bool f[2][N], ans1, ans2;
int n1, n2;
int main(void) {
  // freopen("input.txt","r",stdin);
  s[1] = 'T';
  scanf("%s%d%d", s + 2, &tx, &ty);
  n = strlen(s + 1);
  tx += delta;
  ty += delta;
  m = 0;
  for (int i = 1; i <= n; i++)
    s[i] == 'T' ? ++m : ++a[m];
  swap(n, m);
  n1 = n2 = 0;
  for (int i = 1; i <= n; i += 2) {
    a1[++n1] = a[i];
    a2[++n2] = a[i + 1];
  }
  now = 0, pre = !now;
  memset(f, 0, sizeof(f));
  f[now][delta + a1[1]] = 1;
  for (int i = 2; i <= n1; i++) {
    now ^= 1, pre = !now;
    for (int j = delta - m; j <= delta + m; j++)
      f[now][j] = f[pre][j - a1[i]] | f[pre][j + a1[i]];
  }
  ans1 = f[now][tx];
  now = 0, pre = !now;
  memset(f, 0, sizeof(f));
  f[now][delta] = 1;
  for (int i = 1; i <= n2; i++) {
    now ^= 1, pre = !now;
    for (int j = delta - m; j <= delta + m; j++)
      f[now][j] = f[pre][j - a2[i]] | f[pre][j + a2[i]];
  }
  ans2 = f[now][ty];
  if (ans1 && ans2)
    puts("Yes");
  else
    puts("No");
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define N 40011
#define delta N / 2
using namespace std;
char s[8001] = {};
int n, tx, ty, m;
int a[8001];
int a1[8001] = {}, a2[8001] = {};
int now, pre;
bool f[2][N], ans1, ans2;
int n1, n2;
int main(void) {
  // freopen("input.txt","r",stdin);
  s[1] = 'T';
  scanf("%s%d%d", s + 2, &tx, &ty);
  n = strlen(s + 1);
  tx += delta;
  ty += delta;
  m = 0;
  for (int i = 1; i <= n; i++)
    s[i] == 'T' ? ++m : ++a[m];
  swap(n, m);
  n1 = n2 = 0;
  for (int i = 1; i <= n; i += 2) {
    a1[++n1] = a[i];
    a2[++n2] = a[i + 1];
  }
  now = 0, pre = !now;
  memset(f, 0, sizeof(f));
  f[now][delta + a1[1]] = 1;
  for (int i = 2; i <= n1; i++) {
    now ^= 1, pre = !now;
    for (int j = delta - m; j <= delta + m; j++)
      f[now][j] = f[pre][j - a1[i]] | f[pre][j + a1[i]];
  }
  ans1 = f[now][tx];
  now = 0, pre = !now;
  memset(f, 0, sizeof(f));
  f[now][delta] = 1;
  for (int i = 1; i <= n2; i++) {
    now ^= 1, pre = !now;
    for (int j = delta - m; j <= delta + m; j++)
      f[now][j] = f[pre][j - a2[i]] | f[pre][j + a2[i]];
  }
  ans2 = f[now][ty];
  if (ans1 && ans2)
    puts("Yes");
  else
    puts("No");
  return 0;
}