Problem p03488 (C++)

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

typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)

//* 便利な変数
namespace {
int dx4[] = {1, -1, 0, 0};
int dy4[] = {0, 0, 1, -1};

int dx8[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy8[] = {0, 0, -1, 1, -1, 1, -1, 1};

int mDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;

const ll MOD_CONST = (ll)(1e9 + 7);
} // namespace
template <typename T> vector<T> INP(ll n) {
  vector<T> x;
  REP(i, n) {
    T tmp;
    cin >> tmp;
    x.push_back(tmp);
  }
  return move(x);
}
//* n文字1行の文字列を入力，一文字ごとの配列を返す
vector<char> SPRIT_STRING(ll n) {
  string str;
  cin >> str;
  vector<char> cs(n);
  REP(i, n) cs[i] = str[i];
  return move(cs);
}
//* 文字列中から文字列を検索して別の文字列に置換する
void strReplace(std::string &str, const std::string &from,
                const std::string &to) {
  std::string::size_type pos = 0;
  while (pos = str.find(from, pos), pos != std::string::npos) {
    str.replace(pos, from.length(), to);
    pos += to.length();
  }
}
//* 素数関連関数
namespace PrimeLib {
//* 素数判定 is_prime<unsigned>(N)
template <typename T, std::enable_if_t<std::is_unsigned<T>::value,
                                       std::nullptr_t> = nullptr>
bool is_prime(const T n) {
  if (n < 4)
    return n == 2 || n == 3;
  if (n % 2 == 0 || n % 3 == 0 || (n % 6 != 1 && n % 6 != 5))
    return false;
  for (T i = 5; i * i <= n; i += 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}
//* 素数テーブル生成　エラトステネスの篩
vector<ll> primeTable(ll n) {
  vector<bool> table(n - 1);
  vector<ll> ret;
  for (ll i = 0; i < n - 1; ++i) {
    if (table[i] == false) {
      ll num = i + 2;
      ret.push_back(i + 2);
      for (int j = i + num; j < n - 1; j += num)
        table[j] = true;
    }
  }
  return ret;
}
} // namespace PrimeLib
//* 組み合わせ計算
inline unsigned long long NChooseK(const unsigned long long &n,
                                   const unsigned long long &k) {
  if (n < k)
    return 0;
  if (0 == n)
    return 0;
  if (0 == k)
    return 1;
  if (n == k)
    return 1;
  if (1 == k)
    return n;
  typedef unsigned long long value_type;
  value_type *table = new value_type[static_cast<std::size_t>(n * n)];
  std::fill_n(table, n * n, 0);
  class n_choose_k_impl {
  public:
    n_choose_k_impl(value_type *table, const value_type &dimension)
        : table_(table), dimension_(dimension) {}

    inline value_type &lookup(const value_type &n, const value_type &k) {
      return table_[dimension_ * n + k];
    }

    inline value_type compute(const value_type &n, const value_type &k) {
      if ((0 == k) || (k == n))
        return 1;
      value_type v1 = lookup(n - 1, k - 1);
      if (0 == v1)
        v1 = lookup(n - 1, k - 1) = compute(n - 1, k - 1);
      value_type v2 = lookup(n - 1, k);
      if (0 == v2)
        v2 = lookup(n - 1, k) = compute(n - 1, k);
      return v1 + v2;
    }

    value_type *table_;
    value_type dimension_;
  };
  value_type result = n_choose_k_impl(table, n).compute(n, k);
  delete[] table;
  return result;
}
//* 座標nx, nyがWidth,Heightの領域内にあるかどうかのチェック
inline bool rangeCheck2D(int nx, int ny, int Width, int Height) {
  return nx >= 0 and nx < Width and ny >= 0 and ny < Height;
}
//* bit全探索
/*
        for (int i = 0; i < 1<<N; i++) {
                REP(j, N)
                        if ((1 & i >> j) == 1) {;}
        }
*/
//* 値の二分探索
/*
        int lb = -1, ub = n;
        while (ub - lb > 1) {
                int mid = (lb + ub) / 2;
                if ( ) { // midが条件を満たす
                        ub = mid; // 解の存在範囲は[lb, mid]
                } else {
                        lb = mid; // 解の存在範囲は[mid, ub]
                }
        }
        int ans = ub;
*/
//* 最短路
struct edge {
  int from, to, cost;
};
//* ベルマンフォード法　負の経路があっても動作
//* もし負の閉路があればfalseを返す
bool bellmanFord(vector<edge> &es, vector<ll> &ds, int start) {
  for (auto &d : ds)
    d = INT64_MAX;
  ds[start] = 0;
  for (int n = 0; n < ds.size(); ++n) {
    for (int i = 0; i < es.size(); ++i) {
      edge e = es[i];
      if (ds[e.from] != INT64_MAX and ds[e.to] > ds[e.from] + e.cost) {
        ds[e.to] = ds[e.from] + e.cost;
        if (n == ds.size() - 1)
          return false;
      }
    }
  }
  return true;
}

namespace abc080d {
void abc080d() {
  cin >> N >> C;
  typedef tuple<int, int, int> Show; // channel, start, end
  vector<Show> shows(N);
  REP(i, N) {
    int s, t, c;
    shows[i] = make_tuple(c, s, t);
  }
  sort(shows.begin(), shows.end());
}
} // namespace abc080d

namespace agc015b {
void agc015b() {
  string str;
  cin >> str;
  ll cnt = 0;
  REP(i, str.size()) {
    if (str[i] == 'U') {
      cnt += 2 * i;
      cnt += (str.size() - (i + 1));
    } else {
      cnt += i;
      cnt += 2 * (str.size() - (i + 1));
    }
  }
  cout << cnt << endl;
}
} // namespace agc015b

namespace arc078b {
void arc078b() {
  cin >> N;
  vector<edge> es((N - 1) * 2);
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    es[i].from = a;
    es[i].to = b;
    es[i].cost = 1;
    es[N - 1 + i].from = b;
    es[N - 1 + i].to = a;
    es[N - 1 + i].cost = 1;
  }
  vector<ll> ds(N);

  bellmanFord(es, ds, 0);
  REP(i, N) { cout << ds[i] << endl; }
  cout << ds[N - 1] << endl;
}
} // namespace arc078b

namespace abc088d {
void abc088d() {
  string str;
  cin >> str;
  int ans = INT32_MAX;
  REP(i, str.size()) {
    if (i == str.size() - 1)
      continue;
    if (str[i] == str[i + 1])
      continue;
    int n = i + 1;
    ans = min(max(n, (int)str.size() - n), ans);
  }
  if (ans == INT32_MAX) {
    cout << str.size() << endl;
  } else
    cout << ans << endl;
}
} // namespace abc088d

namespace arc087b {
bool check(int start, int dest, vector<int> ms) {
  bool couldBe[16000];
  REP(i, 16000) couldBe[i] = false;
  couldBe[8000 + start] = true;
  queue<int> pos;
  pos.push(start);
  for (auto xm : ms) {
    int num = pos.size();
    REP(i, num) {
      int p = pos.front();
      pos.pop();
      pos.push(p + xm);
      couldBe[8000 + p + xm] = true;
      pos.push(p - xm);
      couldBe[8000 + p - xm] = true;
    }
  }
  return couldBe[8000 + dest];
}
void arc087b() {
  string str;
  cin >> str;
  int destX, destY;
  cin >> destX >> destY;
  vector<int> xms, yms;
  int sX = 0, sY = 0;
  int index = 0;
  while (str[index++] == 'F')
    sX++;
  bool plusY = true;
  int cnt = 0;
  while (index < str.size()) {
    if (str[index] == 'F') {
      cnt++;
      if (index < str.size() - 1) {
        index++;
        continue;
      }
    }
    if (plusY)
      yms.push_back(cnt);
    else
      xms.push_back(cnt);
    cnt = 0;
    plusY = not plusY;
    index++;
  }
  if (check(sX, destX, xms) and check(sY, destY, yms))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
} // namespace arc087b

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // abc088d::abc088d();
  arc087b::arc087b();
  // agc020a::agc020a();
  return 0;
}

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

typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)

//* 便利な変数
namespace {
int dx4[] = {1, -1, 0, 0};
int dy4[] = {0, 0, 1, -1};

int dx8[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy8[] = {0, 0, -1, 1, -1, 1, -1, 1};

int mDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;

const ll MOD_CONST = (ll)(1e9 + 7);
} // namespace
template <typename T> vector<T> INP(ll n) {
  vector<T> x;
  REP(i, n) {
    T tmp;
    cin >> tmp;
    x.push_back(tmp);
  }
  return move(x);
}
//* n文字1行の文字列を入力，一文字ごとの配列を返す
vector<char> SPRIT_STRING(ll n) {
  string str;
  cin >> str;
  vector<char> cs(n);
  REP(i, n) cs[i] = str[i];
  return move(cs);
}
//* 文字列中から文字列を検索して別の文字列に置換する
void strReplace(std::string &str, const std::string &from,
                const std::string &to) {
  std::string::size_type pos = 0;
  while (pos = str.find(from, pos), pos != std::string::npos) {
    str.replace(pos, from.length(), to);
    pos += to.length();
  }
}
//* 素数関連関数
namespace PrimeLib {
//* 素数判定 is_prime<unsigned>(N)
template <typename T, std::enable_if_t<std::is_unsigned<T>::value,
                                       std::nullptr_t> = nullptr>
bool is_prime(const T n) {
  if (n < 4)
    return n == 2 || n == 3;
  if (n % 2 == 0 || n % 3 == 0 || (n % 6 != 1 && n % 6 != 5))
    return false;
  for (T i = 5; i * i <= n; i += 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}
//* 素数テーブル生成　エラトステネスの篩
vector<ll> primeTable(ll n) {
  vector<bool> table(n - 1);
  vector<ll> ret;
  for (ll i = 0; i < n - 1; ++i) {
    if (table[i] == false) {
      ll num = i + 2;
      ret.push_back(i + 2);
      for (int j = i + num; j < n - 1; j += num)
        table[j] = true;
    }
  }
  return ret;
}
} // namespace PrimeLib
//* 組み合わせ計算
inline unsigned long long NChooseK(const unsigned long long &n,
                                   const unsigned long long &k) {
  if (n < k)
    return 0;
  if (0 == n)
    return 0;
  if (0 == k)
    return 1;
  if (n == k)
    return 1;
  if (1 == k)
    return n;
  typedef unsigned long long value_type;
  value_type *table = new value_type[static_cast<std::size_t>(n * n)];
  std::fill_n(table, n * n, 0);
  class n_choose_k_impl {
  public:
    n_choose_k_impl(value_type *table, const value_type &dimension)
        : table_(table), dimension_(dimension) {}

    inline value_type &lookup(const value_type &n, const value_type &k) {
      return table_[dimension_ * n + k];
    }

    inline value_type compute(const value_type &n, const value_type &k) {
      if ((0 == k) || (k == n))
        return 1;
      value_type v1 = lookup(n - 1, k - 1);
      if (0 == v1)
        v1 = lookup(n - 1, k - 1) = compute(n - 1, k - 1);
      value_type v2 = lookup(n - 1, k);
      if (0 == v2)
        v2 = lookup(n - 1, k) = compute(n - 1, k);
      return v1 + v2;
    }

    value_type *table_;
    value_type dimension_;
  };
  value_type result = n_choose_k_impl(table, n).compute(n, k);
  delete[] table;
  return result;
}
//* 座標nx, nyがWidth,Heightの領域内にあるかどうかのチェック
inline bool rangeCheck2D(int nx, int ny, int Width, int Height) {
  return nx >= 0 and nx < Width and ny >= 0 and ny < Height;
}
//* bit全探索
/*
        for (int i = 0; i < 1<<N; i++) {
                REP(j, N)
                        if ((1 & i >> j) == 1) {;}
        }
*/
//* 値の二分探索
/*
        int lb = -1, ub = n;
        while (ub - lb > 1) {
                int mid = (lb + ub) / 2;
                if ( ) { // midが条件を満たす
                        ub = mid; // 解の存在範囲は[lb, mid]
                } else {
                        lb = mid; // 解の存在範囲は[mid, ub]
                }
        }
        int ans = ub;
*/
//* 最短路
struct edge {
  int from, to, cost;
};
//* ベルマンフォード法　負の経路があっても動作
//* もし負の閉路があればfalseを返す
bool bellmanFord(vector<edge> &es, vector<ll> &ds, int start) {
  for (auto &d : ds)
    d = INT64_MAX;
  ds[start] = 0;
  for (int n = 0; n < ds.size(); ++n) {
    for (int i = 0; i < es.size(); ++i) {
      edge e = es[i];
      if (ds[e.from] != INT64_MAX and ds[e.to] > ds[e.from] + e.cost) {
        ds[e.to] = ds[e.from] + e.cost;
        if (n == ds.size() - 1)
          return false;
      }
    }
  }
  return true;
}

namespace abc080d {
void abc080d() {
  cin >> N >> C;
  typedef tuple<int, int, int> Show; // channel, start, end
  vector<Show> shows(N);
  REP(i, N) {
    int s, t, c;
    shows[i] = make_tuple(c, s, t);
  }
  sort(shows.begin(), shows.end());
}
} // namespace abc080d

namespace agc015b {
void agc015b() {
  string str;
  cin >> str;
  ll cnt = 0;
  REP(i, str.size()) {
    if (str[i] == 'U') {
      cnt += 2 * i;
      cnt += (str.size() - (i + 1));
    } else {
      cnt += i;
      cnt += 2 * (str.size() - (i + 1));
    }
  }
  cout << cnt << endl;
}
} // namespace agc015b

namespace arc078b {
void arc078b() {
  cin >> N;
  vector<edge> es((N - 1) * 2);
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    es[i].from = a;
    es[i].to = b;
    es[i].cost = 1;
    es[N - 1 + i].from = b;
    es[N - 1 + i].to = a;
    es[N - 1 + i].cost = 1;
  }
  vector<ll> ds(N);

  bellmanFord(es, ds, 0);
  REP(i, N) { cout << ds[i] << endl; }
  cout << ds[N - 1] << endl;
}
} // namespace arc078b

namespace abc088d {
void abc088d() {
  string str;
  cin >> str;
  int ans = INT32_MAX;
  REP(i, str.size()) {
    if (i == str.size() - 1)
      continue;
    if (str[i] == str[i + 1])
      continue;
    int n = i + 1;
    ans = min(max(n, (int)str.size() - n), ans);
  }
  if (ans == INT32_MAX) {
    cout << str.size() << endl;
  } else
    cout << ans << endl;
}
} // namespace abc088d

namespace arc087b {
bool check(int start, int dest, vector<int> ms) {
  static bool couldBe[8005][17000];
  REP(j, 8005) REP(i, 17000) couldBe[j][i] = false;
  couldBe[0][8005 + start] = true;
  REP(i, ms.size()) {
    int m = ms[i];
    REP(j, 17000) {
      int p1 = j - m;
      int p2 = j + m;
      couldBe[i + 1][j] =
          (p1 >= 0 and couldBe[i][p1]) or (p2 < 17000 and couldBe[i][p2]);
    }
  }
  return couldBe[ms.size()][8005 + dest];
}
void arc087b() {
  string str;
  cin >> str;
  int destX, destY;
  cin >> destX >> destY;
  vector<int> xms, yms;
  int sX = 0, sY = 0;
  int index = 0;
  while (str[index++] == 'F')
    sX++;
  bool plusY = true;
  int cnt = 0;
  while (index < str.size()) {
    if (str[index] == 'F') {
      cnt++;
      if (index < str.size() - 1) {
        index++;
        continue;
      }
    }
    if (plusY)
      yms.push_back(cnt);
    else
      xms.push_back(cnt);
    cnt = 0;
    plusY = not plusY;
    index++;
  }
  if (check(sX, destX, xms) and check(sY, destY, yms))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
} // namespace arc087b

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // abc088d::abc088d();
  arc087b::arc087b();
  // agc020a::agc020a();
  return 0;
}