Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
inline bool Yes(bool condition) {
  if (condition)
    PRINT("Yes");
  else
    PRINT("No");
}
template <class itr> void cins(itr first, itr last) {
  for (auto i = first; i != last; i++) {
    cin >> (*i);
  }
}
template <class itr> void array_output(itr start, itr goal) {
  string ans = "";
  for (auto i = start; i != goal; i++)
    ans += to_string(*i) + " ";
  if (!ans.empty())
    ans.pop_back();
  PRINT(ans);
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
const ll INF = 1e17;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;
const ll MAX = 200005;
void dp(vector<ll> &a, vector<bool> &b) {
  if (a.size() == 0)
    return;
  rep(i, (int)a.size()) {
    vector<bool> copy(16001, 0);
    rep(j, (int)b.size()) {
      if (b[j]) {
        if (j - a[i] >= 0)
          copy[j - a[i]] = 1;
        if (j + a[i] <= 16000)
          copy[j + a[i]] = 1;
      }
    }
    rep(j, 16001) b[j] = copy[j];
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  s += 'T';
  ll x, y, stock = 0;
  cin >> x >> y;
  ll flag = 1;
  bool f = 1;
  vector<ll> u, l;
  rep(i, (int)s.length()) {
    if (s[i] == 'F') {
      if (f)
        x -= 1;
      else
        stock++;
    } else {
      if (f) {
        f = 0;
        continue;
      }
      if (flag)
        u.push_back(stock);
      else
        l.push_back(stock);
      flag = 1 - flag;
      stock = 0;
    }
  }
  // array_output(all(u));
  // array_output(all(l));
  vector<bool> canl(16001, 0), canu(16001, 0);
  canl[8000] = 1;
  canu[8000] = 1;
  dp(l, canl);
  dp(u, canu);
  if (canl[8000 + x] && canu[8000 + y])
    PRINT("Yes");
  else
    PRINT("No");
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
inline bool Yes(bool condition) {
  if (condition)
    PRINT("Yes");
  else
    PRINT("No");
}
template <class itr> void cins(itr first, itr last) {
  for (auto i = first; i != last; i++) {
    cin >> (*i);
  }
}
template <class itr> void array_output(itr start, itr goal) {
  string ans = "";
  for (auto i = start; i != goal; i++)
    ans += to_string(*i) + " ";
  if (!ans.empty())
    ans.pop_back();
  PRINT(ans);
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
const ll INF = 1e17;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;
const ll MAX = 200005;
void dp(vector<ll> &a, vector<bool> &b) {
  if (a.size() == 0)
    return;
  rep(i, (int)a.size()) {
    vector<bool> copy(16001, 0);
    rep(j, (int)b.size()) {
      if (b[j]) {
        if (j - a[i] >= 0)
          copy[j - a[i]] = 1;
        if (j + a[i] <= 16000)
          copy[j + a[i]] = 1;
      }
    }
    rep(j, 16001) b[j] = copy[j];
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  s += 'T';
  ll x, y, stock = 0;
  cin >> x >> y;
  ll flag = 1;
  bool f = 1;
  vector<ll> u, l;
  rep(i, (int)s.length()) {
    if (s[i] == 'F') {
      if (f)
        x -= 1;
      else
        stock++;
    } else {
      if (f) {
        f = 0;
        continue;
      }
      if (flag)
        u.push_back(stock);
      else
        l.push_back(stock);
      flag = 1 - flag;
      stock = 0;
    }
  }
  // array_output(all(u));
  // array_output(all(l));
  vector<bool> canl(16001, 0), canu(16001, 0);
  canl[8000] = 1;
  canu[8000] = 1;
  dp(l, canl);
  dp(u, canu);
  if (x < -8000)
    PRINT("No");
  else if ((canl[8000 + x] && canu[8000 + y]))
    PRINT("Yes");
  else
    PRINT("No");
}