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

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

string S;
int dp[510][510];

bool rec(int l, int r) {
  cerr << l << " " << r << endl;
  if (dp[l][r] != -1)
    return dp[l][r];
  dp[l][r] = 0;
  if (l > r)
    return dp[l][r] = 1;
  if (S[l] != 'm' || S[r] != 'w')
    return dp[l][r] = 0;
  for (int i = l + 1; i < r; i++) {
    if (S[i] == 'e')
      dp[l][r] = (dp[l][r] | (rec(l + 1, i - 1) & rec(i + 1, r - 1)));
  }
  return dp[l][r];
}

int main() {
  cin >> S;
  int N = S.size();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      dp[i][j] = -1;
  cout << (rec(0, N - 1) ? "Cat" : "Rabbit") << endl;
  /*for(int i=0;i<N;i++){
      for(int j=i;j<N;j++) cerr << dp[i][j] << " ";
      cerr << endl;
  }*/
}


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

string S;
int dp[510][510];

bool rec(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  dp[l][r] = 0;
  if (l > r)
    return dp[l][r] = 1;
  if (S[l] != 'm' || S[r] != 'w')
    return dp[l][r] = 0;
  for (int i = l + 1; i < r; i++) {
    if (S[i] == 'e')
      dp[l][r] = (dp[l][r] | (rec(l + 1, i - 1) & rec(i + 1, r - 1)));
  }
  return dp[l][r];
}

int main() {
  cin >> S;
  int N = S.size();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      dp[i][j] = -1;
  cout << (rec(0, N - 1) ? "Cat" : "Rabbit") << endl;
  /*for(int i=0;i<N;i++){
      for(int j=i;j<N;j++) cerr << dp[i][j] << " ";
      cerr << endl;
  }*/
}
Problem p01486 (C++)

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

bool iscat(const string &S_) {
  map<string, int> vis;
  function<bool(const string &)> dfs = [&](const string &S) {
    if (S.empty())
      return true;
    if (S[0] != 'm' || S.back() != 'w')
      return false;
    if (vis.count(S))
      return vis[S] == 1;
    rep(i, S.size()) if (S[i] == 'e') {
      if (iscat(S.substr(1, i - 1)) &&
          iscat(S.substr(i + 1, S.size() - i - 2))) {
        vis[S] = 1;
        return true;
      }
    }
    vis[S] = 0;
    return false;
  };
  return dfs(S_);
}

int main() {
  string S;
  cin >> S;
  cout << (iscat(S) ? "Cat" : "Rabbit") << endl;
}

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

bool iscat(const string &S_) {
  map<string, int> vis;
  function<bool(const string &)> dfs = [&](const string &S) {
    if (S.empty())
      return true;
    if (S[0] != 'm' || S.back() != 'w')
      return false;
    if (vis.count(S))
      return vis[S] == 1;
    rep(i, S.size()) if (S[i] == 'e') {
      if (dfs(S.substr(1, i - 1)) && dfs(S.substr(i + 1, S.size() - i - 2))) {
        vis[S] = 1;
        return true;
      }
    }
    vis[S] = 0;
    return false;
  };
  return dfs(S_);
}

int main() {
  string S;
  cin >> S;
  cout << (iscat(S) ? "Cat" : "Rabbit") << endl;
}
Problem p01486 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
#include <string>

using namespace std;

void catChecker(string str) {

  stack<char> cnt;

  for (int i = 0; i < str.size(); i++) {
    char c = str.at(i);

    if (c == 'm') {
      cnt.push(c);
    } else if (c == 'e' && cnt.top() == 'm') {
      cnt.pop();
      cnt.push(c);
    } else if (c == 'w' && cnt.top() == 'e') {
      cnt.pop();
    } else {
      cout << "Rabbit" << endl;
      return;
    }

    if (i < str.size() - 1 && cnt.empty()) {
      cout << "Rabbit" << endl;
      return;
    }
  }
  if (cnt.empty()) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
  return;
}

int main(void) {

  string str;

  while (cin >> str)
    catChecker(str);

  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
#include <string>

using namespace std;

void catChecker(string str) {

  stack<char> cnt;

  for (int i = 0; i < str.size(); i++) {
    char c = str.at(i);

    if (c == 'm') {
      cnt.push(c);
    } else if (!cnt.empty()) {
      if (c == 'e' && cnt.top() == 'm') {
        cnt.pop();
        cnt.push(c);
      } else if (c == 'w' && cnt.top() == 'e') {
        cnt.pop();
      } else {
        cout << "Rabbit" << endl;
        return;
      }
    } else {
      cout << "Rabbit" << endl;
      return;
    }

    if (i < str.size() - 1 && cnt.empty()) {
      cout << "Rabbit" << endl;
      return;
    }
  }
  if (cnt.empty()) {
    cout << "Cat" << endl;
  } else {
    cout << "Rabbit" << endl;
  }
  return;
}

int main(void) {

  string str;

  while (cin >> str)
    catChecker(str);

  return 0;
}
Problem p01486 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s;
  cin >> s;
  stack<char> st;
  bool ans = true;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'w' && st.top() == 'e' && st.size() > 1 &&
        (i == s.length() - 1 || s[i + 1] != 'm')) {
      st.pop();
      st.pop();
    } else {
      st.push(s[i]);
    }
    if (st.empty() && i < s.length() - 1) {
      ans = false;
    }
  }
  if (!st.empty())
    ans = false;
  if (ans)
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s;
  cin >> s;
  stack<char> st;
  bool ans = true;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'w' && st.size() > 1 && st.top() == 'e' &&
        (i == s.length() - 1 || s[i + 1] != 'm')) {
      st.pop();
      st.pop();
    } else {
      st.push(s[i]);
    }
    if (st.empty() && i < s.length() - 1) {
      ans = false;
    }
  }
  if (!st.empty())
    ans = false;
  if (ans)
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}
Problem p01486 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

bool dfs(string s) {
  int res = 0;
  if (!s.size())
    return 1;
  if (s[0] != 'm' || s[s.size() - 1] != 'w')
    return 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != 'e')
      continue;
    string a = s.substr(1, i - 1);
    string b = s.substr(i + 1);
    b.erase(b.end() - 1);
    if (dfs(a) && dfs(b))
      return 1;
  }
  return 0;
}

int main() {
  string s;
  cin >> s;
  puts(dfs(s) ? "Cat" : "Rabbit");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

bool dfs(string s) {
  int res = 0;
  if (!s.size())
    return 1;
  if (s[0] != 'm' || s[s.size() - 1] != 'w')
    return 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != 'e')
      continue;
    string a = s.substr(1, i - 1);
    string b = s.substr(i + 1);
    b.erase(b.end() - 1);
    if (dfs(a) && dfs(b))
      return 1;
  }
  return 0;
}

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

  while (1) {
    if (s.empty()) {
      puts("Cat");
      break;
    }

    string::size_type p = s.find("mew");
    if (p == string::npos) {
      puts("Rabbit");
      break;
    } else {
      if (!(s.size() == 3 || (s[p - 1] == 'm' && s[p + 3] == 'e') ||
            (s[p - 1] == 'e' && s[p + 3] == 'w'))) {
        puts("Rabbit");
        break;
      }
      s.erase(p, 3);
    }
  }
  return 0;
}
Problem p01486 (C++)

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

char str[1024];
bool f[1024][1024];

int main() {
  scanf("%s", str);
  int n = strlen(str);
  for (int i = 0; i < n; i++) {
    f[i][i] = true;
  }
  for (int t = 0; t < n; t++) {
    for (int w1 = 0; w1 < n; w1 += 3) {
      for (int w2 = 0; w2 < n; w2 += 3) {
        for (int i = 0; i < n; i++) {
          int j = i + w1 + 1;
          int k = i + w1 + w2 + 2;
          if (str[i] != 'm')
            continue;
          if (str[j] != 'e')
            continue;
          if (str[k] != 'w')
            continue;
          if (!f[i + 1][j])
            continue;
          if (!f[j + 1][k])
            continue;
          f[i][k + 1] = true;
        }
      }
    }
  }
  puts(f[0][n] ? "Cat" : "Rabbit");
}

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

char str[1024];
bool f[1024][1024];

int main() {
  scanf("%s", str);
  int n = strlen(str);
  for (int i = 0; i < n; i++) {
    f[i][i] = true;
  }
  for (int t = 0; t < n / 2; t++) {
    for (int w1 = 0; w1 < n; w1 += 3) {
      for (int w2 = 0; w2 < n; w2 += 3) {
        for (int i = 0; i < n; i++) {
          int j = i + w1 + 1;
          int k = i + w1 + w2 + 2;
          if (str[i] != 'm')
            continue;
          if (str[j] != 'e')
            continue;
          if (str[k] != 'w')
            continue;
          if (!f[i + 1][j])
            continue;
          if (!f[j + 1][k])
            continue;
          f[i][k + 1] = true;
        }
      }
    }
  }
  puts(f[0][n] ? "Cat" : "Rabbit");
}
Problem p01491 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef long long ll;
ll a[2500000], b[2500000], sa[2500001], sb[2500001];
ll M, N, m0, md, n0, nd, ans = 1e18;
ll val(ll i, ll j) { return (M - i) * (N - j) + sa[i] + sb[j]; }
int main() {
  cin >> M >> N >> m0 >> md >> n0 >> nd;
  a[0] = m0;
  rep(i, M - 1) a[i + 1] = (a[i] * 58 + md) % (N + 1);
  b[0] = n0;
  rep(i, N - 1) b[i + 1] = (b[i] * 58 + nd) % (M + 1);
  sort(a, a + M);
  sort(b, b + N);
  rep(i, M) sa[i + 1] = sa[i] + a[i];
  rep(i, N) sb[i + 1] = sb[i] + b[i];
  ll j = N, now = sb[N];
  rep(i, M + 1) {
    while (j > 0 && val(i, j) > val(i, j - 1)) {
      j--;
    }
    ans = min(ans, val(i, j));
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef long long ll;
int a[2500000], b[2500000];
ll M, N, m0, md, n0, nd, ans = 1e18, sa[2500001], sb[2500001];
ll val(ll i, ll j) { return (M - i) * (N - j) + sa[i] + sb[j]; }
int main() {
  cin >> M >> N >> m0 >> md >> n0 >> nd;
  a[0] = m0;
  rep(i, M - 1) a[i + 1] = (a[i] * 58 + md) % (N + 1);
  b[0] = n0;
  rep(i, N - 1) b[i + 1] = (b[i] * 58 + nd) % (M + 1);
  sort(a, a + M);
  sort(b, b + N);
  rep(i, M) sa[i + 1] = sa[i] + a[i];
  rep(i, N) sb[i + 1] = sb[i] + b[i];
  ll j = N, now = sb[N];
  rep(i, M + 1) {
    while (j > 0 && val(i, j) > val(i, j - 1)) {
      j--;
    }
    ans = min(ans, val(i, j));
  }
  cout << ans << endl;
}
Problem p01491 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll m, n, m0, md, n0, nd;
vi sum_m, sum_n;

ll calc(int pm, int pn) { return sum_m[pm] + sum_n[pn] + (m - pm) * (n - pn); }

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cin >> m >> n >> m0 >> md >> n0 >> nd;
  vi v(m), w(n);
  v[0] = m0;
  REP(i, m - 1) v[i + 1] = (v[i] * 58 + md) % (n + 1);
  w[0] = n0;
  REP(i, n - 1) w[i + 1] = (w[i] * 58 + nd) % (m + 1);
  sort(ALL(v));
  sort(ALL(w));
  sum_m.resize(m + 1);
  sum_n.resize(n + 1);
  REP(i, m) sum_m[i + 1] = sum_m[i] + v[i];
  REP(i, n) sum_n[i + 1] = sum_n[i] + w[i];
  ll ans = LLONG_MAX;
  int pm = 0, pn = n;
  while (1) {
    ll tmp = calc(pm, pn);
    chmin(ans, tmp);
    while (1) {
      if (pn == 0)
        break;
      ll nx = calc(pm, pn - 1);
      if (tmp < nx)
        break;
      pn--;
      tmp = nx;
      chmin(ans, tmp);
    }
    pm++;
    if (pn < 0 || pm > m)
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll m, n, m0, md, n0, nd;
vi sum_m, sum_n;

ll calc(int pm, int pn) { return sum_m[pm] + sum_n[pn] + (m - pm) * (n - pn); }

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cin >> m >> n >> m0 >> md >> n0 >> nd;
  vector<int> v(m), w(n);
  v[0] = m0;
  REP(i, m - 1) v[i + 1] = (v[i] * 58 + md) % (n + 1);
  w[0] = n0;
  REP(i, n - 1) w[i + 1] = (w[i] * 58 + nd) % (m + 1);
  sort(ALL(v));
  sort(ALL(w));
  sum_m.resize(m + 1);
  sum_n.resize(n + 1);
  REP(i, m) sum_m[i + 1] = sum_m[i] + v[i];
  REP(i, n) sum_n[i + 1] = sum_n[i] + w[i];
  ll ans = LLONG_MAX;
  int pm = 0, pn = n;
  while (1) {
    ll tmp = calc(pm, pn);
    chmin(ans, tmp);
    while (1) {
      if (pn == 0)
        break;
      ll nx = calc(pm, pn - 1);
      if (tmp < nx)
        break;
      pn--;
      tmp = nx;
      chmin(ans, tmp);
    }
    pm++;
    if (pn < 0 || pm > m)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p01493 (C++)

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

int N;
bool visited[100];
char a[100][100];
int cnt = 0, k = 0;
int cc;
int t[2];

void dfs(int pos) {
  if (visited[pos])
    return;
  visited[pos] = true;
  cc++;
  for (int i = 0; i < N; i++)
    if (a[pos][i] == 'Y')
      dfs(i);
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'Y')
        cnt++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (visited[i])
      continue;
    k++;
    cc = 0;
    dfs(i);
    t[cc % 2]++;
  }
  cnt /= 2;

  int total = N * (N - 1) / 2;

  if (N % 2 == 1 || t[1] % 2 == 1) {
    if ((total - cnt) % 2 == 1)
      cout << "Taro" << endl;
    else
      cout << "Hanako" << endl;
    return 0;
  }

  if (t[0] == 0 && t[1] == 2) {
    if ((total - cnt) % 2 == 0)
      cout << "Taro" << endl;
    else
      cout << "Hanako" << endl;
    return 0;
  }

  if (t[0] == 1 && t[1] == 2) {
    cout << "Taro" << endl;
    return 0;
  }

  if ((total - cnt) % 2 == 1)
    cout << "Taro" << endl;
  else
    cout << "Hanako" << endl;

  return 0;
}

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

int N;
bool visited[1005];
char a[1005][1005];
int cnt = 0, k = 0;
int cc;
int t[2];

void dfs(int pos) {
  if (visited[pos])
    return;
  visited[pos] = true;
  cc++;
  for (int i = 0; i < N; i++)
    if (a[pos][i] == 'Y')
      dfs(i);
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'Y')
        cnt++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (visited[i])
      continue;
    k++;
    cc = 0;
    dfs(i);
    t[cc % 2]++;
  }
  cnt /= 2;

  int total = N * (N - 1) / 2;

  if (N % 2 == 1 || t[1] % 2 == 1) {
    if ((total - cnt) % 2 == 1)
      cout << "Taro" << endl;
    else
      cout << "Hanako" << endl;
    return 0;
  }

  if (t[0] == 0 && t[1] == 2) {
    if ((total - cnt) % 2 == 0)
      cout << "Taro" << endl;
    else
      cout << "Hanako" << endl;
    return 0;
  }

  if (t[0] == 1 && t[1] == 2) {
    cout << "Taro" << endl;
    return 0;
  }

  if ((total - cnt) % 2 == 1)
    cout << "Taro" << endl;
  else
    cout << "Hanako" << endl;

  return 0;
}
Problem p01499 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n, t;
int level[10010];
const ll MOD = 1e+9 + 7;

int main() {
  while (scanf("%d %d", &n, &t) > 0) {
    REP(i, n) { scanf("%d", &level[i]); }
    sort(level, level + n);
    ll ans = 1;
    deque<int> prev;
    REP(i, n) {
      while (!prev.empty() && prev.front() < level[i] - t) {
        prev.pop_front();
      }
      ans = (ans * (prev.size() + 1)) % MOD;
      prev.push_back(level[i]);
    }
    printf("%lld\n", ans);
  }
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n, t;
int level[100010];
const ll MOD = 1e+9 + 7;

int main() {
  while (scanf("%d %d", &n, &t) > 0) {
    REP(i, n) { scanf("%d", &level[i]); }
    sort(level, level + n);
    ll ans = 1;
    deque<int> prev;
    REP(i, n) {
      while (!prev.empty() && prev.front() < level[i] - t) {
        prev.pop_front();
      }
      ans = (ans * (prev.size() + 1)) % MOD;
      prev.push_back(level[i]);
    }
    printf("%lld\n", ans);
  }
}
Problem p01499 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define each(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = (int)1e9;
const double EPS = 1e-9, INF = 1e12;

const int mod = (int)1e9 + 7;
int n, t, d[10000];

int main() {
  cin >> n >> t;
  rep(i, n) cin >> d[i];
  sort(d, d + n);
  ll ans = 1;

  for (int i = 0, j = 0; j < n; j++) {
    while (i < j && d[i] + t < d[j])
      i++;
    ans = ans * (j - i + 1) % mod;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define each(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = (int)1e9;
const double EPS = 1e-9, INF = 1e12;

const int mod = (int)1e9 + 7;
int n, t, d[100000];

int main() {
  cin >> n >> t;
  rep(i, n) cin >> d[i];
  sort(d, d + n);
  ll ans = 1;

  for (int i = 0, j = 0; j < n; j++) {
    while (i < j && d[i] + t < d[j])
      i++;
    ans = ans * (j - i + 1) % mod;
  }
  cout << ans << endl;

  return 0;
}
Problem p01499 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < b ? (a = b, 1) : 0)
#define chmin(a, b) (a > b ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int a[10000];

int main() {
  int n, t;
  cin >> n >> t;
  REP(i, n) cin >> a[i];
  sort(a, a + n);
  ll ans = 1;
  const ll mod = 1e9 + 7;
  REP(i, n) {
    ll num = upper_bound(a, a + n, a[i] + t) - (a + i);
    ans = ans * num % mod;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < b ? (a = b, 1) : 0)
#define chmin(a, b) (a > b ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int a[1000000];

int main() {
  int n, t;
  cin >> n >> t;
  REP(i, n) cin >> a[i];
  sort(a, a + n);
  ll ans = 1;
  const ll mod = 1e9 + 7;
  REP(i, n) {
    ll num = upper_bound(a, a + n, a[i] + t) - (a + i);
    ans = ans * num % mod;
  }
  cout << ans << endl;
}
Problem p01502 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

bool used[100];
long long c[101][101], R;
int n;

long long solve(int k, long long res) {
  if (res < 0)
    return 1e15;
  vector<pair<int, int>> next;
  rep(i, n) if (!used[i]) next.push_back(make_pair(c[k][i], i));
  if (next.size() == 0u)
    return 0;
  sort(next.begin(), next.end());
  rep(i, next.size()) {
    used[next[i].second] = true;
    res = min(solve(next[i].second, res - next[i].first) + next[i].first, res);
    used[next[i].second] = false;
  }
  return res;
}

int main() {
  cin >> n;
  rep(i, n) rep(j, n) cin >> c[i][j];
  long long res = 0;
  rep(i, n) rep(j, i) {
    long long m = min(c[i][j], c[j][i]);
    res += m;
    c[i][j] -= m;
    c[j][i] -= m;
  }
  cout << solve(100, 1e15) + res << endl;
  return 0;
}

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

using namespace std;

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

bool used[100];
long long c[101][101], R;
int n;

long long solve(int k, long long res) {
  if (res <= 0)
    return 1e15;
  vector<pair<int, int>> next;
  rep(i, n) if (!used[i]) next.push_back(make_pair(c[k][i], i));
  if (next.size() == 0u)
    return 0;
  sort(next.begin(), next.end());
  rep(i, next.size()) {
    used[next[i].second] = true;
    res = min(solve(next[i].second, res - next[i].first) + next[i].first, res);
    used[next[i].second] = false;
  }
  return res;
}

int main() {
  cin >> n;
  rep(i, n) rep(j, n) cin >> c[i][j];
  long long res = 0;
  rep(i, n) rep(j, i) {
    long long m = min(c[i][j], c[j][i]);
    res += m;
    c[i][j] -= m;
    c[j][i] -= m;
  }
  cout << solve(100, 1e15) + res << endl;
  return 0;
}
Problem p01520 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

signed main() {
  int n, t, e;
  cin >> n >> t >> e;
  vi x(n);
  rep(i, 0, n) cin >> x[i];
  int ans = -1;
  rep(i, 0, n) {
    for (int j = 1;; j++) {
      if (t - e <= j * x[i] && j * x[i] <= t + e) {
        ans = i + 1;
        i = n;
      }
      if (j * x[i] > t + e)
        break;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

signed main() {
  int n, t, e;
  cin >> n >> t >> e;
  vi x(n);
  rep(i, 0, n) cin >> x[i];
  int ans = -1;
  rep(i, 0, n) {
    for (int j = 1;; j++) {
      if (t - e <= j * x[i] && j * x[i] <= t + e) {
        ans = i + 1;
        i = n;
        break;
      }
      if (j * x[i] > t + e)
        break;
    }
  }
  cout << ans << endl;
}
Problem p01522 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>

using namespace std;

int main() {
  int n, k, r;
  int m[50], p[50], q[50];
  int bunny[50][50];
  int count = 0;
  set<int> all, selected;
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    cin >> m[i];
    for (int j = 0; j < m[i]; ++j) {
      cin >> bunny[i][j];
    }
  }
  cin >> r;
  for (int i = 0; i < r; ++i) {
    cin >> p[i] >> q[i];
  }
  for (int i = 0; i < k; ++i) {
    all.clear();
    selected.clear();
    for (int j = 0; j < m[i]; ++j) {
      all.insert(bunny[i][j]);
    }
    for (int j = 0; j < r; ++j) {
      if (all.count(p[j]) * all.count(q[j]) > 0) {
        selected.insert(p[j]);
        selected.insert(q[j]);
      }
    }
    count += selected.size();
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <set>

using namespace std;

int main() {
  int n, k, r;
  int m[50], p[5000], q[5000];
  int bunny[50][50];
  int count = 0;
  set<int> all, selected;
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    cin >> m[i];
    for (int j = 0; j < m[i]; ++j) {
      cin >> bunny[i][j];
    }
  }
  cin >> r;
  for (int i = 0; i < r; ++i) {
    cin >> p[i] >> q[i];
  }
  for (int i = 0; i < k; ++i) {
    all.clear();
    selected.clear();
    for (int j = 0; j < m[i]; ++j) {
      all.insert(bunny[i][j]);
    }
    for (int j = 0; j < r; ++j) {
      if (all.count(p[j]) * all.count(q[j]) > 0) {
        selected.insert(p[j]);
        selected.insert(q[j]);
      }
    }
    count += selected.size();
  }
  cout << count << endl;
  return 0;
}
Problem p01523 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int n, m, a[100], b[100];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d%d", &a[i], &b[i]);
  int l = 1, c = 0;
  while (l <= n) {
    int ptr = -1;
    for (int j = 0; j < m; j++) {
      if (a[j] <= l)
        ptr = j;
    }
    if (ptr == -1) {
      printf("Impossible\n");
      return 0;
    }
    l = b[ptr] + 1;
    c++;
  }
  printf("%d\n", c);
  return 0;
}

Fixed submission:
#include <stdio.h>
int n, m, a[100], b[100];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    scanf("%d%d", &a[i], &b[i]);
  int l = 1, c = 0;
  while (l <= n) {
    int ptr = -1;
    for (int j = 0; j < m; j++) {
      if (a[j] <= l && b[j] >= l && b[ptr] < b[j])
        ptr = j;
    }
    if (ptr == -1) {
      printf("Impossible\n");
      return 0;
    }
    l = b[ptr] + 1;
    c++;
  }
  printf("%d\n", c);
  return 0;
}
Problem p01529 (C++)

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

int main() {
  ll N;
  cin >> N;
  vector<ll> w(N), sum(N, 0);
  for (ll i = 0; i < N; i++) {
    cin >> w[i];
    sum[i + 1] = sum[i] + w[i];
  }
  vector<vector<ll>> dp(N, vector<ll>(N, -1)), mo(N, vector<ll>(N));
  for (ll i = 0; i < N; i++) {
    dp[i][i] = 0;
    mo[i][i] = i;
  }
  for (ll i = 0; i + 1 < N; i++) {
    dp[i][i + 1] = sum[i + 2] - sum[i];
    mo[i][i + 1] = i;
  }

  for (ll l = 2; l < N; l++) {
    for (ll i = 0; i + l < N; i++) {
      for (ll k = mo[i][i + l - 1]; k <= mo[i + 1][i + l]; k++) {
        if (dp[i][i + l] == -1 || dp[i][i + l] > dp[i][k] + dp[k + 1][i + l]) {
          dp[i][i + l] = dp[i][k] + dp[k + 1][i + l];
          mo[i][i + l] = k;
        }
      }
      dp[i][i + l] += sum[i + l + 1] - sum[i];
    }
  }
  cout << dp[0][N - 1] << endl;
  return 0;
}

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

int main() {
  ll N;
  cin >> N;
  vector<ll> w(N), sum(N + 1, 0);
  for (ll i = 0; i < N; i++) {
    cin >> w[i];
    sum[i + 1] = sum[i] + w[i];
  }
  vector<vector<ll>> dp(N, vector<ll>(N, -1)), mo(N, vector<ll>(N));
  for (ll i = 0; i < N; i++) {
    dp[i][i] = 0;
    mo[i][i] = i;
  }
  for (ll i = 0; i + 1 < N; i++) {
    dp[i][i + 1] = sum[i + 2] - sum[i];
    mo[i][i + 1] = i;
  }

  for (ll l = 2; l < N; l++) {
    for (ll i = 0; i + l < N; i++) {
      for (ll k = mo[i][i + l - 1]; k <= mo[i + 1][i + l]; k++) {
        if (dp[i][i + l] == -1 || dp[i][i + l] > dp[i][k] + dp[k + 1][i + l]) {
          dp[i][i + l] = dp[i][k] + dp[k + 1][i + l];
          mo[i][i + l] = k;
        }
      }
      dp[i][i + l] += sum[i + l + 1] - sum[i];
    }
  }
  cout << dp[0][N - 1] << endl;
  return 0;
}
Problem p01529 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

#define MAX_N (100)

using namespace std;

typedef long long lint;

int main() {
  int N;
  lint w[MAX_N], rSum[MAX_N];
  static lint dp[MAX_N][MAX_N];
  static int pos[MAX_N][MAX_N];

  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    scanf("%lld", w + i);
    rSum[i] = i ? rSum[i - 1] + w[i] : w[i];
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = (lint)1e16;
    }
    dp[i][i] = 0;
    pos[i][i] = i;
  }

  for (int w = 1; w < N; w++) {
    for (int i = 0, j; (j = i + w) < N; i++) {
      for (int k = pos[i][j - 1]; k <= pos[i + 1][j]; k++) {
        lint nc = dp[i][k] + dp[k + 1][j] + rSum[j] - (i ? rSum[i - 1] : 0);
        if (nc < dp[i][j]) {
          dp[i][j] = nc;
          pos[i][j] = k;
        }
      }
    }
  }

  printf("%lld\n", dp[0][N - 1]);

  return (0);
}

Fixed submission:
#include <algorithm>
#include <cstdio>

#define MAX_N (4000)

using namespace std;

typedef long long lint;

int main() {
  int N;
  lint w[MAX_N], rSum[MAX_N];
  static lint dp[MAX_N][MAX_N];
  static int pos[MAX_N][MAX_N];

  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    scanf("%lld", w + i);
    rSum[i] = i ? rSum[i - 1] + w[i] : w[i];
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = (lint)1e16;
    }
    dp[i][i] = 0;
    pos[i][i] = i;
  }

  for (int w = 1; w < N; w++) {
    for (int i = 0, j; (j = i + w) < N; i++) {
      for (int k = pos[i][j - 1]; k <= pos[i + 1][j]; k++) {
        lint nc = dp[i][k] + dp[k + 1][j] + rSum[j] - (i ? rSum[i - 1] : 0);
        if (nc < dp[i][j]) {
          dp[i][j] = nc;
          pos[i][j] = k;
        }
      }
    }
  }

  printf("%lld\n", dp[0][N - 1]);

  return (0);
}
Problem p01529 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define INF (1LL << 61)

using namespace std;

typedef unsigned long long ull;

int main(void) {

  int n;
  cin >> n;

  vector<ull> cost(n);
  for (int i = 0; i < n; i++) {
    cin >> cost[i];
    if (i > 0)
      cost[i] += cost[i - 1];
  }

  static ull dp[4000][4000], k[4000][4000];
  fill(dp[0], dp[n + 1], INF);
  fill(k[0], k[n + 1], INF);

  for (int i = 0; i < n; i++) {
    dp[i][i] = 0;
    k[i][i] = i;
  }

  for (int w = 1; w <= n; w++) {
    for (int i = 0, j = i + w; j < n; i++, j++) {
      for (int r = k[i][j - 1]; r <= k[i + 1][j]; r++) {
        ull c = dp[i][r] + dp[r + 1][j] + cost[j] - ((i > 0) ? cost[i - 1] : 0);
        if (dp[i][j] > c)
          dp[i][j] = c, k[i][j] = r;
      }
    }
  }
  cout << dp[0][n - 1] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define INF (1LL << 61)

using namespace std;

typedef unsigned long long ull;

int main(void) {

  int n;
  cin >> n;

  vector<ull> cost(n);
  for (int i = 0; i < n; i++) {
    cin >> cost[i];
    if (i > 0)
      cost[i] += cost[i - 1];
  }

  static ull dp[4001][4001], k[4001][4001];
  fill(dp[0], dp[4001], INF);

  for (int i = 0; i < n; i++) {
    dp[i][i] = 0;
    k[i][i] = i;
  }

  for (int w = 1; w <= n; w++) {
    for (int i = 0, j = i + w; j < n; i++, j++) {
      for (int r = k[i][j - 1]; r <= k[i + 1][j]; r++) {
        ull c = dp[i][r] + dp[r + 1][j] + cost[j] - ((i > 0) ? cost[i - 1] : 0);
        if (dp[i][j] > c)
          dp[i][j] = c, k[i][j] = r;
      }
    }
  }
  cout << dp[0][n - 1] << endl;

  return 0;
}
Problem p01529 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
const int inf = 1e9;
const ll linf = 1LL << 50;
int n;
ll W[4001];
ll dp[4001][4001];
ll sum[4001];
int at[4001][4001];

int main(int argc, char const *argv[]) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", W + i);
    if (i == 0)
      sum[i] = W[i];
    else
      sum[i] = sum[i - 1] + W[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      at[i][j] = i;
      dp[i][j] = linf;
    }
    dp[i][i] = 0;
  }
  for (int w = 1; w < n; w++) {
    for (int i = 0; i + w < n; i++) {
      for (int k = max(at[i][i + w - 1], i); k < i + w; k++) {
        if (dp[i][i + w] > dp[i][k] + dp[k + 1][i + w]) {
          dp[i][i + w] = dp[i][k] + dp[k + 1][i + w];
          at[i][i + w] = k;
        } else if (dp[i][i + w] < dp[i][k] + dp[k + 1][i + w]) {
          // break;
        }
      }
      if (i != 0)
        dp[i][i + w] += sum[i + w] - sum[i - 1];
      else
        dp[i][i + w] += sum[i + w];
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
const int inf = 1e9;
const ll linf = 1LL << 50;
int n;
ll W[4001];
ll dp[4001][4001];
ll sum[4001];
int at[4001][4001];

int main(int argc, char const *argv[]) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", W + i);
    if (i == 0)
      sum[i] = W[i];
    else
      sum[i] = sum[i - 1] + W[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      at[i][j] = i;
      dp[i][j] = linf;
    }
    dp[i][i] = 0;
  }
  for (int w = 1; w < n; w++) {
    for (int i = 0; i + w < n; i++) {
      for (int k = max(at[i][i + w - 1], i);
           k <= min(i + w - 1, at[i + 1][i + w]); k++) {
        if (dp[i][i + w] > dp[i][k] + dp[k + 1][i + w]) {
          dp[i][i + w] = dp[i][k] + dp[k + 1][i + w];
          at[i][i + w] = k;
        } else if (dp[i][i + w] < dp[i][k] + dp[k + 1][i + w]) {
          // break;
        }
      }
      if (i != 0)
        dp[i][i + w] += sum[i + w] - sum[i - 1];
      else
        dp[i][i + w] += sum[i + w];
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}
Problem p01532 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<int, int> P;
const int MAX = 100;
P data[MAX];
int N, T, H, L;
int mon;

void solve() {
  int player = -1;

  while (1) {
    player++;
    if (player == N)
      player = 0;
    if (data[player].first > 0) {
      mon += 10;
      T++;
      data[player].first--;
      if (L < T) {
        cout << player + 1 << endl;
        break;
      }
    } else if (data[player].second > 0) {
      mon += 100;
      H++;
      data[player].second--;
    } else {
      cout << player + 1 << endl;
      break;
    }

    if (mon >= 90) {
      mon -= 90;
      T -= 9;
      data[player].first += 9;
      if (T < 0) {
        cout << player + 1 << endl;
        break;
      }
    }
  }
}

void input() {

  cin >> N >> T >> H >> L;

  for (int i = 0; i < N; i++)
    cin >> data[i].first >> data[i].second;
}

int main() {
  input();
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<int, int> P;
const int MAX = 100;
P data[MAX];
int N, T, H, L;
int mon;

void solve() {
  int player = -1;

  while (1) {
    player++;
    if (player == N)
      player = 0;
    if (data[player].first > 0) {
      mon += 10;
      T++;
      data[player].first--;
      if (L < T) {
        cout << player + 1 << endl;
        break;
      }
    } else if (data[player].second > 0) {
      mon += 100;
      H++;
      data[player].second--;
    } else {
      cout << player + 1 << endl;
      break;
    }

    if (mon >= 90) {
      mon -= 90;
      T -= mon / 10;
      data[player].first += mon / 10;
      mon = 0;
      if (T < 0) {
        cout << player + 1 << endl;
        break;
      }
    }
  }
}

void input() {

  cin >> N >> T >> H >> L;

  for (int i = 0; i < N; i++)
    cin >> data[i].first >> data[i].second;
}

int main() {
  input();
  solve();
  return 0;
}
Problem p01534 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int n, m, a[55], b[55], c[55], dp[55][55][55][55];
bool vis[55][55][55][55];
int rec(int pos, int rem, int lv2, int lv1) {
  if (pos == n)
    return 0;
  if (vis[pos][rem][lv2][lv1])
    return dp[pos][rem][lv2][lv1];
  int ret = -999999999;
  for (int i = 0; i <= rem; i++) {
    int res = rec(pos + 1, rem - i, i, lv2);
    int cs = c[pos];
    for (int j = 1; j <= 8; j++) {
      for (int k = 0; k <= j; k++) {
        int l = j - k;
        if (k <= lv2 && l <= lv1) {
          cs = max(cs, a[pos] * k + b[pos] * l);
        }
      }
    }
    ret = max(ret, res + cs);
  }
  return dp[pos][rem][lv2][lv1] = ret;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  int ret = -999999999;
  for (int i = 0; i <= m; i++)
    ret = max(ret, rec(0, m - i, i, 0));
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int n, m, a[55], b[55], c[55], dp[55][55][55][55];
bool vis[55][55][55][55];
int rec(int pos, int rem, int lv2, int lv1) {
  if (pos == n)
    return 0;
  if (vis[pos][rem][lv2][lv1])
    return dp[pos][rem][lv2][lv1];
  int ret = -999999999;
  for (int i = 0; i <= rem; i++) {
    int res = rec(pos + 1, rem - i, i, lv2);
    int cs = c[pos];
    for (int j = 1; j <= 8; j++) {
      for (int k = 0; k <= j; k++) {
        int l = j - k;
        if (k <= lv2 && l <= lv1) {
          cs = max(cs, a[pos] * k + b[pos] * l);
        }
      }
    }
    ret = max(ret, res + cs);
  }
  vis[pos][rem][lv2][lv1] = true;
  return dp[pos][rem][lv2][lv1] = ret;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  int ret = -999999999;
  for (int i = 0; i <= m; i++)
    ret = max(ret, rec(0, m - i, i, 0));
  cout << ret << endl;
  return 0;
}
Problem p01534 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

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

int n, m;
int x[51], y[51], z[51];
int dp1[51][51][51];
int dp2[51][51][51];
const int INF = 1e9;
int cost(int a, int b, int c) {
  if (dp1[a][b][c] != -INF)
    return dp1[a][b][c];
  int res = -INF;
  rep(i, b + 1) rep(j, c + 1) {
    if (i == 0 && j == 0)
      res = max(res, z[a]);
    else if (i + j <= 8)
      res = max(res, i * x[a] + j * y[a]);
  }
  return dp1[a][b][c] = res;
}
int dfs(int a, int b, int c) {
  if (a == n)
    return 0;
  // if (dp2[a][b][c] != -INF) return dp2[a][b][c];
  // int res = dfs(a + 1, b, 0) + z[a];
  int res = -INF;

  rep(i, b + 1) {
    int s = -INF;
    rep(j, i + 1) {
      rep(k, c + 1) {
        if (k == 0 && j == 0)
          s = max(z[a], s);
        else if (j + k <= 8)
          s = max(s, j * x[a] + k * y[a]);
      }
    }
    res = max(res, dfs(a + 1, b - i, i) + s);
  }

  // rep(i, b + 1) { res = max(res, dfs(a + 1, b - i, i) + cost(a, i, c)); }

  return dp2[a][b][c] = res;
}
int main() {
  rep(i, 51) rep(j, 51) rep(k, 51) dp1[i][j][k] = -INF;
  rep(i, 51) rep(j, 51) rep(k, 51) dp2[i][j][k] = -INF;
  cin >> n >> m;
  rep(i, n) cin >> x[i] >> y[i] >> z[i];
  cout << dfs(0, m, 0) << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

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

int n, m;
int x[51], y[51], z[51];
int dp1[51][51][51];
int dp2[51][51][51];
const int INF = 1e9;
int cost(int a, int b, int c) {
  if (dp1[a][b][c] != -INF)
    return dp1[a][b][c];
  int res = -INF;
  rep(i, b + 1) rep(j, c + 1) {
    if (i == 0 && j == 0)
      res = max(res, z[a]);
    else if (i + j <= 8)
      res = max(res, i * x[a] + j * y[a]);
  }
  return dp1[a][b][c] = res;
}
int dfs(int a, int b, int c) {
  if (a == n)
    return 0;
  if (dp2[a][b][c] != -INF)
    return dp2[a][b][c];
  // int res = dfs(a + 1, b, 0) + z[a];
  int res = -INF;

  rep(i, b + 1) {
    int s = -INF;
    rep(j, i + 1) {
      rep(k, c + 1) {
        if (k == 0 && j == 0)
          s = max(z[a], s);
        else if (j + k <= 8)
          s = max(s, j * x[a] + k * y[a]);
      }
    }
    res = max(res, dfs(a + 1, b - i, i) + s);
  }

  // rep(i, b + 1) { res = max(res, dfs(a + 1, b - i, i) + cost(a, i, c)); }

  return dp2[a][b][c] = res;
}
int main() {
  rep(i, 51) rep(j, 51) rep(k, 51) dp1[i][j][k] = -INF;
  rep(i, 51) rep(j, 51) rep(k, 51) dp2[i][j][k] = -INF;
  cin >> n >> m;
  rep(i, n) cin >> x[i] >> y[i] >> z[i];
  cout << dfs(0, m, 0) << endl;
}
Problem p01538 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;

vector<int> ans_list;

bool dp[1000000];

int main() {

  int Q;
  cin >> Q;

  memset(dp, 0, sizeof(dp));

  while (Q) {
    Q--;
    int N;
    cin >> N;
    int ans = 0;
    memset(dp, 0, sizeof(dp));

    while (N / 10 > 0) {
      if (dp[N]) {
        ans = -1;
        break;
      }
      int tmp = 0;
      for (int i = 1; N / pow(10, i) > 0; i++) {
        tmp = max(tmp, (N / (int)pow(10, i)) * (N % (int)pow(10, i)));
      }
      dp[N] = false;
      N = tmp;
      ans++;
    }

    ans_list.push_back(ans);
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;

vector<int> ans_list;

bool dp[1000001];

int main() {

  int Q;
  cin >> Q;

  memset(dp, 0, sizeof(dp));

  while (Q) {
    Q--;
    int N;
    cin >> N;
    int ans = 0;
    memset(dp, 0, sizeof(dp));

    while (N / 10 > 0) {
      if (dp[N]) {
        ans = -1;
        break;
      }
      int tmp = 0;
      for (int i = 1; N / pow(10, i) > 0; i++) {
        tmp = max(tmp, (N / (int)pow(10, i)) * (N % (int)pow(10, i)));
      }
      dp[N] = false;
      N = tmp;
      ans++;
    }

    ans_list.push_back(ans);
  }

  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }

  return 0;
}
Problem p01538 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {
  int q, n;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n;
    int cnt = 0;
    while (n >= 10) {
      int tmp = -1;
      string ns = to_string(n);
      for (int i = 0; i < ns.size() - 1; i++) {
        cerr << ns.substr(0, i + 1) << " " << ns.substr(i + 1) << endl;
        tmp = max(tmp, stoi(ns.substr(0, i + 1)) * stoi(ns.substr(i + 1)));
      }
      // cerr << tmp << endl;
      n = tmp;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

// EOF

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int main(void) {
  int q, n;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n;
    int cnt = 0;
    while (n >= 10) {
      int tmp = -1;
      string ns = to_string(n);
      for (int i = 0; i < ns.size() - 1; i++) {
        // cerr << ns.substr(0, i+1) << " " << ns.substr(i+1) << endl;
        tmp = max(tmp, stoi(ns.substr(0, i + 1)) * stoi(ns.substr(i + 1)));
      }
      // cerr << tmp << endl;
      n = tmp;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

// EOF