Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (signed)(a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define MOD 1000000007
#define pb push_back
#define INF 93193111451418101
#define MIN -93193111451418101
#define EPS 1e-11
#define tp(a, b, c) make_tuple(a, b, c)
#define lb(a, b) lower_bound((a).begin(), (a).end(), (b))
#define ub(a, b) upper_bound((a).begin(), (a).end(), (b))
using namespace std;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
//------------------変数-----------------------//
//-------------------関数----------------------//
ll dp[2][20000];

int main() {
  ll x, y;
  string s;
  cin >> s >> x >> y;
  ll c = 0;
  std::vector<int> v;
  REP(i, s.size()) {
    if (s[i] == 'T') {
      v.pb(c);
      c = 0;
    } else {
      c++;
    }
  }
  v.pb(c);
  dp[0][8003] = 1; // y
  ll m = 1;
  FOR(i, 0, v.size()) {
    if (i % 2 == 0) {
      continue;
    }
    ll mod = i % 4 == 1 ? 0 : 1;
    REP(j, 17000) {
      if (!dp[mod][j])
        continue;
      dp[1 - mod][j + v[i]] = 1;
      dp[1 - mod][j - v[i]] = 1;
    }
    //  cout << "hoge" << endl;
    memset(dp[mod], 0, sizeof(dp[mod]));
    m = mod;
  }
  if (!(dp[1 ^ m][y + 8003])) {
    // cout << "a" << endl;
    cout << "No" << endl;
    return 0;
  }
  fill_all(dp, (ll)0);
  dp[1][8003] = 1; // y
  FOR(i, 0, v.size()) {
    if (i == 0) {
      continue;
    }
    if (i % 2 == 1) {
      continue;
    }
    ll mod = i % 4 == 0 ? 0 : 1;

    REP(j, 17000) {
      if (!dp[mod][j])
        continue;
      dp[1 ^ mod][j + v[i]] = 1;
      dp[1 ^ mod][j - v[i]] = 1;
    }
    memset(dp[mod], 0, sizeof(dp[mod]));
    m = mod;
  }
  if (!(dp[1 ^ m][x - v[0] + 8003])) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (signed)(a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define MOD 1000000007
#define pb push_back
#define INF 93193111451418101
#define MIN -93193111451418101
#define EPS 1e-11
#define tp(a, b, c) make_tuple(a, b, c)
#define lb(a, b) lower_bound((a).begin(), (a).end(), (b))
#define ub(a, b) upper_bound((a).begin(), (a).end(), (b))
using namespace std;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
//------------------変数-----------------------//
//-------------------関数----------------------//
ll dp[2][20000];

int main() {
  ll x, y;
  string s;
  cin >> s >> x >> y;
  ll c = 0;
  std::vector<int> v;
  REP(i, s.size()) {
    if (s[i] == 'T') {
      v.pb(c);
      c = 0;
    } else {
      c++;
    }
  }
  v.pb(c);
  dp[0][8003] = 1; // y
  ll m = 1;
  FOR(i, 0, v.size()) {
    if (i % 2 == 0) {
      continue;
    }
    ll mod = i % 4 == 1 ? 0 : 1;
    REP(j, 17000) {
      if (!dp[mod][j])
        continue;
      dp[1 - mod][j + v[i]] = 1;
      dp[1 - mod][j - v[i]] = 1;
    }
    //  cout << "hoge" << endl;
    memset(dp[mod], 0, sizeof(dp[mod]));
    m = mod;
  }
  if (!(dp[1 ^ m][y + 8003])) {
    // cout << "a" << endl;
    cout << "No" << endl;
    return 0;
  }
  m = 0;
  fill_all(dp, (ll)0);
  dp[1][8003] = 1; // y
  FOR(i, 0, v.size()) {
    if (i == 0) {
      continue;
    }
    if (i % 2 == 1) {
      continue;
    }
    ll mod = i % 4 == 0 ? 0 : 1;

    REP(j, 17000) {
      if (!dp[mod][j])
        continue;
      dp[1 ^ mod][j + v[i]] = 1;
      dp[1 ^ mod][j - v[i]] = 1;
    }
    memset(dp[mod], 0, sizeof(dp[mod]));
    m = mod;
  }
  if (!(dp[1 ^ m][x - v[0] + 8003])) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
}