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

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

int main() {
  int N;
  vector<int> F;

  cin >> N;
  for (int i = 0; i < N + 1; i++) {
    F.push_back(0);
  }

  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;

    int xn = max(0, x - 1);
    F[xn]++;
    if (y <= N) {
      F[y]--;
    }
  }

  for (int i = 0; i < N; i++) {
    F[i + 1] += F[i];
  }

  int ans;
  for (int i = 0; i < N + 1; i++) {
    if (F[i] >= i) {
      ans = i;
    }
  }

  cout << ans << endl;

  return 0;
}

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

int main() {
  int N;
  vector<int> F;

  cin >> N;
  for (int i = 0; i < N + 1; i++) {
    F.push_back(0);
  }

  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;

    int xn = max(0, x - 1);
    if (xn <= N) {
      F[xn]++;
    }
    if (y <= N) {
      F[y]--;
    }
  }

  for (int i = 0; i < N; i++) {
    F[i + 1] += F[i];
  }

  int ans;
  for (int i = 0; i < N + 1; i++) {
    if (F[i] >= i) {
      ans = i;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p01449 (C++)

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

int main() {
  int n;
  std::cin >> n;
  vector<int> p(n), cell(n, 1e9);
  for (int i = 0; i < n; i++) {
    std::cin >> p[i];
  }
  queue<pii> que;
  cell[0] = 0;
  que.push(pii(0, 0));
  while (not que.empty()) {
    int s = que.front().first;
    int cost = que.front().second;
    que.pop();
    for (int i = 1; i <= 6; i++) {
      int pos = s + i;
      bool flag = false;
      for (int j = 0; j < 2 * n; j++) {
        pos = min(n - 1, pos);
        if (p[pos] != 0) {
          cell[pos] = cost;
          pos += p[pos];
        } else {
          break;
        }
        if (j == 2 * n - 1)
          flag = true;
      }
      pos = min(n - 1, pos);
      if (flag == true)
        continue;
      if (cell[pos] > cost + 1) {
        cell[pos] = cost + 1;
        que.push(pii(pos, cost + 1));
      }
    }
  }

  std::cout << cell[n - 1] << std::endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

int main() {
  int n;
  std::cin >> n;
  vector<int> p(n), cell(n, 1e9);
  for (int i = 0; i < n; i++) {
    std::cin >> p[i];
  }
  queue<pii> que;
  cell[0] = 0;
  que.push(pii(0, 0));
  while (not que.empty()) {
    int s = que.front().first;
    int cost = que.front().second;
    que.pop();
    for (int i = 1; i <= 6; i++) {
      int pos = s + i;
      bool flag = false;
      for (int j = 0; j < 2 * n; j++) {
        pos = min(n - 1, pos);
        if (p[pos] != 0 and cell[pos] > cost) {
          cell[pos] = cost;
          pos += p[pos];
        } else {
          break;
        }
        if (j == 2 * n - 1)
          flag = true;
      }
      pos = min(n - 1, pos);
      if (flag == true)
        continue;
      if (cell[pos] > cost + 1) {
        cell[pos] = cost + 1;
        que.push(pii(pos, cost + 1));
      }
    }
  }

  std::cout << cell[n - 1] << std::endl;
  return 0;
}
Problem p01449 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;

vector<int> T;
vector<int> M;
vector<bool> check;
int N;
const int NOT = -2;

int calc(int idx) {
  if (T[idx] == NOT)
    return NOT;
  if (T[idx] >= 0)
    return T[idx];
  if (check[idx]) {
    T[idx] = NOT;
    return NOT;
  }
  check[idx] = true;
  if (M[idx] == 0) {
    T[idx] = idx;
    return idx;
  }
  return T[idx] = calc(idx + M[idx]);
}

class Node {
public:
  int idx, cost;
  Node(int idx, int cost) : idx(idx), cost(cost) {}
  Node();
};

bool operator<(Node a, Node b) { return a.cost < b.cost; }

int main() {
  cin >> N;
  T.resize(N, -1);
  check.resize(N, false);
  M.resize(N);
  for (int i = 0; i < N; i++)
    cin >> M[i];

  T[0] = 0;
  T[N - 1] = N - 1;
  for (int i = N - 1; i >= 1; i--) {
    if (!check[i])
      calc(i);
  }
  vector<int> ans(N, N + 1);
  priority_queue<Node> que;
  que.push(Node(0, 0));

  //	cout<<endl;
  int res = N + 1;
  while (!que.empty()) {
    Node now = que.top();
    que.pop();
    //		cout<<now.idx<<endl;
    if (T[now.idx] == NOT)
      continue;
    if (ans[now.idx] <= now.cost)
      continue;
    ans[now.idx] = now.cost;
    for (int i = 1; i <= 6; i++) {
      if (now.idx + i <= N - 1) {
        if (T[now.idx + i] >= 0)
          que.push(Node(T[now.idx + i], now.cost + 1));
      }
    }
  }
  cout << ans[N - 1] << endl;
  return 0;
}

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

vector<int> T;
vector<int> M;
vector<bool> check;
int N;
const int NOT = -2;

int calc(int idx) {
  if (T[idx] == NOT)
    return NOT;
  if (T[idx] >= 0)
    return T[idx];
  if (check[idx]) {
    T[idx] = NOT;
    return NOT;
  }
  check[idx] = true;
  if (M[idx] == 0) {
    T[idx] = idx;
    return idx;
  }
  return T[idx] = calc(idx + M[idx]);
}

class Node {
public:
  int idx, cost;
  Node(int idx, int cost) : idx(idx), cost(cost) {}
  Node();
};

bool operator<(Node a, Node b) { return a.cost > b.cost; }

int main() {
  cin >> N;
  T.resize(N, -1);
  check.resize(N, false);
  M.resize(N);
  for (int i = 0; i < N; i++)
    cin >> M[i];

  T[0] = 0;
  T[N - 1] = N - 1;
  for (int i = N - 1; i >= 1; i--) {
    if (!check[i])
      calc(i);
  }
  vector<int> ans(N, N + 1);
  priority_queue<Node> que;
  que.push(Node(0, 0));

  //	cout<<endl;
  int res = N + 1;
  while (!que.empty()) {
    Node now = que.top();
    que.pop();
    //		cout<<now.idx<<endl;
    if (T[now.idx] == NOT)
      continue;
    if (ans[now.idx] <= now.cost)
      continue;
    ans[now.idx] = now.cost;
    for (int i = 1; i <= 6; i++) {
      if (now.idx + i <= N - 1) {
        if (T[now.idx + i] >= 0)
          que.push(Node(T[now.idx + i], now.cost + 1));
      }
    }
  }
  cout << ans[N - 1] << endl;
  return 0;
}
Problem p01449 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <queue>
using namespace std;

int main() {
  int N, p[100000], v[100000];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    v[i] = (1 << 24);
  }

  queue<int> q;
  q.push(0);
  v[0] = 0;

  while (!q.empty()) {
    int t = q.front();
    q.pop();

    if (t == N - 1) {
      cout << v[t] << endl;
      break;
    }

    for (int d = 1; d <= 6; d++) {
      int nt = t + d;
      if (nt >= N)
        continue;

      while (v[t] + 1 < v[nt]) {
        v[nt] = v[t] + 1;
        nt += p[nt];
      }
      if (p[nt] == 0) {
        q.push(nt);
      }
    }
  }
}

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

int main() {
  int N, p[100000], v[100000];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    v[i] = (1 << 24);
  }

  queue<int> q;
  q.push(0);
  v[0] = 0;

  while (!q.empty()) {
    int t = q.front();
    q.pop();

    if (t == N - 1) {
      cout << v[t] << endl;
      break;
    }

    for (int d = 1; d <= 6; d++) {
      int nt = t + d;
      if (nt >= N)
        continue;

      while (v[t] + 1 < v[nt]) {
        v[nt] = v[t] + 1;
        nt += p[nt];
      }
      if (p[nt] == 0 && v[nt] == v[t] + 1) {
        q.push(nt);
      }
    }
  }
}
Problem p01449 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <map>
#include <queue>

#define fst first
#define snd second

using namespace std;

typedef pair<int, int> que;

int p[100500];
char tbl[100500];

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &p[i]);
  priority_queue<que> q;
  q.push(que(-0, 0));
  while (!q.empty()) {
    que t = q.top();
    q.pop();
    int cost = -t.fst;
    int i = t.snd;

    tbl[i] = 1;
    if (i == n - 1) {
      printf("%d\n", cost);
      break;
    }
    if (p[i]) {
      if (0 <= i + p[i] && i + p[i] < n && !tbl[i + p[i]])
        q.push(que(-cost, i + p[i]));
    } else
      for (int dice = 1; dice <= 6; dice++)
        if (i + dice < n)
          q.push(que(-(cost + 1), i + dice));
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <map>
#include <queue>

#define fst first
#define snd second

using namespace std;

typedef pair<int, int> que;

int p[100500];
char tbl[100500];

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &p[i]);
  priority_queue<que> q;
  q.push(que(-0, 0));
  while (!q.empty()) {
    que t = q.top();
    q.pop();
    int cost = -t.fst;
    int i = t.snd;

    tbl[i] = 1;
    if (i == n - 1) {
      printf("%d\n", cost);
      break;
    }
    if (p[i]) {
      if (0 <= i + p[i] && i + p[i] < n && !tbl[i + p[i]])
        q.push(que(-cost, i + p[i]));
    } else
      for (int dice = 1; dice <= 6; dice++)
        if (i + dice < n && !tbl[i + dice])
          q.push(que(-(cost + 1), i + dice));
  }
  return 0;
}
Problem p01450 (C++)

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

typedef long long int ll;

const int MAX_N = 205;
const int MAX_W = 10005;
const ll MOD = 1000000007;
int N, W;

ll dp[MAX_N][MAX_W];

ll func(vector<ll> &vec, int sum, int id) {
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= W; j++)
      dp[i][j] = 0;
  dp[id + 1][sum] = 1;

  ll ret = 0;
  for (int i = id + 1; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (dp[i][j] == 0)
        continue;
      dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % MOD;
      if (j + vec[i] > W)
        continue;
      dp[i + 1][j + vec[i]] = (dp[i + 1][j + vec[i]] + dp[i][j]) % MOD;
    }
  }

  for (int i = 0; i <= W; i++) {
    if (W - i < vec[id]) {
      ret = (ret + dp[N][i]) % MOD;
    }
  }
  return ret;
}

int main() {
  cin >> N >> W;
  vector<ll> vec(N);

  for (int i = 0; i < N; i++) {
    cin >> vec[i];
  }

  sort(vec.begin(), vec.end());
  int sum = 0;
  ll res = 0;

  for (int i = 0; i < N; i++) {
    res = (res + func(vec, sum, i)) % MOD;
    sum += vec[i];
  }
  if (sum <= W)
    res = (res + 1LL) % MOD;
  cout << res << endl;
  return 0;
}

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

typedef long long int ll;

const int MAX_N = 205;
const int MAX_W = 10005;
const ll MOD = 1000000007;
int N, W;

ll dp[MAX_N][MAX_W];

ll func(vector<ll> &vec, int sum, int id) {
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= W; j++)
      dp[i][j] = 0;
  dp[id + 1][sum] = 1;

  ll ret = 0;
  for (int i = id + 1; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (dp[i][j] == 0)
        continue;
      dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % MOD;
      if (j + vec[i] > W)
        continue;
      dp[i + 1][j + vec[i]] = (dp[i + 1][j + vec[i]] + dp[i][j]) % MOD;
    }
  }

  for (int i = 0; i <= W; i++) {
    if (W - i < vec[id]) {
      ret = (ret + dp[N][i]) % MOD;
    }
  }
  return ret;
}

int main() {
  cin >> N >> W;
  vector<ll> vec(N);

  for (int i = 0; i < N; i++) {
    cin >> vec[i];
  }

  sort(vec.begin(), vec.end());
  int sum = 0;
  ll res = 0;

  for (int i = 0; i < N; i++) {
    if (sum > W)
      continue;
    res = (res + func(vec, sum, i)) % MOD;
    sum += vec[i];
  }
  if (sum <= W)
    res = (res + 1LL) % MOD;
  cout << res << endl;
  return 0;
}
Problem p01450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.1415926535897932384626433832795028841971
#define INF 1000000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<double, double> P;
typedef pair<double, P> PQ;

int n, w;
int a[200];
ll dp[10001];
ll ans;

int main() {
  cin >> n >> w;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  if (a[0] > w) {
    cout << 1 << endl;
    return 0;
  }
  int sum = 0;
  rep(i, n) sum += a[i];
  if (sum <= w)
    ans++;
  rep(i, n) {
    memset(dp, 0, sizeof(dp));
    int ini = 0;
    int mini = a[i];
    for (int j = 0; j < i; j++) {
      ini += a[j];
    }
    dp[ini] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int k = w - a[j]; k >= 0; k--) {
        dp[k + a[j]] += dp[k];
      }
    }
    for (int j = 0; j <= w; j++)
      dp[j] %= MOD;
    for (int j = w - mini + 1; j <= w; j++)
      ans += dp[j];
    ans %= MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.1415926535897932384626433832795028841971
#define INF 1000000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<double, double> P;
typedef pair<double, P> PQ;

int n, w;
int a[200];
ll dp[10001];
ll ans;

int main() {
  cin >> n >> w;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  if (a[0] > w) {
    cout << 1 << endl;
    return 0;
  }
  int sum = 0;
  rep(i, n) sum += a[i];
  if (sum <= w)
    ans++;
  rep(i, n) {
    memset(dp, 0, sizeof(dp));
    int ini = 0;
    int mini = a[i];
    for (int j = 0; j < i; j++) {
      ini += a[j];
    }
    if (ini > w)
      break;
    dp[ini] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int k = w - a[j]; k >= 0; k--) {
        dp[k + a[j]] += dp[k];
      }
    }
    for (int j = 0; j <= w; j++)
      dp[j] %= MOD;
    for (int j = w - mini + 1; j <= w; j++)
      ans += dp[j];
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p01450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

#define int long long
const int M = 1000000007;
int dp[205][10005];

signed main() {
  int n, w;
  cin >> n >> w;

  vector<int> a(n + 1, -1);
  rep(i, n) cin >> a[i + 1];
  sort(all(a));

  vector<int> sum(all(a));
  sum[0] = 0;
  range(i, 1, n + 1) { sum[i] += sum[i - 1]; }

  dp[0][0] = 1;
  range(i, 1, n + 1) { //???????????¨?????????????????\????????¨???
    if (sum[i] > w)
      break;
    dp[0][sum[i]] = 1;
  }

  range(i, 1, n + 1) { // i - 1????????????????????¨??¨??\??????i
                       // ????????????????????\???????????¨???
    dp[i][sum[i - 1]] = 1;
    range(j, i + 1, n + 1) {
      for (int k = w; k >= sum[i - 1]; k--) {
        if (k + a[j] > w)
          continue;
        // show(k)
        (dp[i][k + a[j]] += dp[i][k]) %= M;
      }
    }
  }

  // rep(i,n + 1){ rep(j,w + 1){ cout << dp[i][j] <<' '; } cout << endl; }

  int ans = 0;
  range(i, 1, n + 1) {
    range(j, sum[i], w + 1) {
      if (j + a[i] <= w)
        continue;
      // show(dp[i][j])
      // cout << i << ' ' << j << endl;
      (ans += dp[i][j]) %= M;
    }
  }

  cout << ans + 1 << endl; //????????????????????\????????¨??????1??????
}

Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

#define int long long
const int M = 1000000007;
int dp[205][10005];

signed main() {
  int n, w;
  cin >> n >> w;

  vector<int> a(n + 1, -1);
  rep(i, n) cin >> a[i + 1];
  sort(all(a));

  vector<int> sum(all(a));
  sum[0] = 0;
  range(i, 1, n + 1) { sum[i] += sum[i - 1]; }

  dp[0][0] = 1;
  range(i, 1, n + 1) { //???????????¨?????????????????\????????¨???
    if (sum[i] > w)
      break;
    dp[0][sum[i]] = 1;
  }

  range(i, 1, n + 1) { // i - 1????????????????????¨??¨??\??????i
                       // ????????????????????\???????????¨???
    if (sum[i] > w)
      break;
    dp[i][sum[i - 1]] = 1;
    range(j, i + 1, n + 1) {
      for (int k = w; k >= sum[i - 1]; k--) {
        if (k + a[j] > w)
          continue;
        // show(k)
        (dp[i][k + a[j]] += dp[i][k]) %= M;
      }
    }
  }

  // rep(i,n + 1){ rep(j,w + 1){ cout << dp[i][j] <<' '; } cout << endl; }

  int ans = 0;
  range(i, 1, n + 1) {
    range(j, sum[i], w + 1) {
      if (j + a[i] <= w)
        continue;
      // show(dp[i][j])
      // cout << i << ' ' << j << endl;
      (ans += dp[i][j]) %= M;
    }
  }

  cout << ans + 1 << endl; //????????????????????\????????¨??????1??????
}
Problem p01450 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

const int mod = 1000000007;
int a[100000 + 10];
int dp[200][100000 + 10];
int w, n;

int memo;
int solve(int pos, int rest) {
  if (pos == n)
    return rest < a[memo] ? 1 : 0;

  if (dp[pos][rest] != -1)
    return dp[pos][rest];

  int ret = 0;
  if (pos >= memo)
    ret = (ret + solve(pos + 1, rest)) % mod;
  if (pos != memo && rest >= a[pos])
    ret = (ret + solve(pos + 1, rest - a[pos])) % mod;

  return dp[pos][rest] = ret;
}

int main() {
  int ans = 0;
  n = getInt();
  w = getInt();

  REP(i, n) a[i] = getInt();

  sort(a, a + n);

  int sum = 0;
  REP(i, n) {
    memset(dp, -1, sizeof(dp));
    memo = i;
    ans = (ans + solve(0, w)) % mod;
    sum += a[i];
    if (sum > w)
      break;
  }
  if (sum <= w)
    ans++;
  if (ans == 0)
    ans = 1;

  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

const int mod = 1000000007;
int a[10000 + 10];
int dp[200][10000 + 10];
int w, n;

int memo;
int solve(int pos, int rest) {
  if (pos == n)
    return rest < a[memo] ? 1 : 0;

  if (dp[pos][rest] != -1)
    return dp[pos][rest];

  int ret = 0;
  if (pos >= memo)
    ret = (ret + solve(pos + 1, rest)) % mod;
  if (pos != memo && rest >= a[pos])
    ret = (ret + solve(pos + 1, rest - a[pos])) % mod;

  return dp[pos][rest] = ret;
}

int main() {
  int ans = 0;
  n = getInt();
  w = getInt();

  REP(i, n) a[i] = getInt();

  sort(a, a + n);

  int sum = 0;
  REP(i, n) {
    memset(dp, -1, sizeof(dp));
    memo = i;
    ans = (ans + solve(0, w)) % mod;
    sum += a[i];
    if (sum > w)
      break;
  }
  if (sum <= w)
    ans++;
  if (ans == 0)
    ans = 1;

  printf("%d\n", ans);

  return 0;
}
Problem p01450 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define All(A) A.begin(), A.end()
#define RAll(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<double, double> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const LL INF = 1 << 30;

LL dp[100001][201];

int main() {
  int N;
  int W;
  cin >> N >> W;
  vector<int> w(N);
  for (int i = 0; i < N; i++) {
    cin >> w[i];
  }
  sort(All(w));
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = W; j >= 0; j--) {
      for (int k = 0; k <= i; k++) {
        if (dp[j][k] == 0)
          continue;
        if (k == 0) {
          if (j + w[i] <= W)
            dp[j + w[i]][k] = (dp[j + w[i]][k] + dp[j][k]) % mod;
          dp[j][i + 1] = (dp[j][i + 1] + dp[j][k]) % mod;
          dp[j][k] = 0;
        } else {
          if (j + w[i] <= W)
            dp[j + w[i]][k] = (dp[j + w[i]][k] + dp[j][k]) % mod;
        }
      }
    }
  }
  LL ans = 0;
  for (int i = 0; i <= W; i++) {
    for (int j = 0; j <= N; j++) {
      if (j == 0) {
        ans = (ans + dp[i][j]) % mod;
      } else {
        if (w[j - 1] > W - i) {
          ans = (ans + dp[i][j]) % mod;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define All(A) A.begin(), A.end()
#define RAll(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<double, double> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const LL INF = 1 << 30;

int dp[100001][201];

int main() {
  int N;
  int W;
  cin >> N >> W;
  vector<int> w(N);
  for (int i = 0; i < N; i++) {
    cin >> w[i];
  }
  sort(All(w));
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = W; j >= 0; j--) {
      for (int k = 0; k <= i; k++) {
        if (dp[j][k] == 0)
          continue;
        if (k == 0) {
          if (j + w[i] <= W)
            dp[j + w[i]][k] = (dp[j + w[i]][k] + dp[j][k]) % mod;
          dp[j][i + 1] = (dp[j][i + 1] + dp[j][k]) % mod;
          dp[j][k] = 0;
        } else {
          if (j + w[i] <= W)
            dp[j + w[i]][k] = (dp[j + w[i]][k] + dp[j][k]) % mod;
        }
      }
    }
  }
  LL ans = 0;
  for (int i = 0; i <= W; i++) {
    for (int j = 0; j <= N; j++) {
      if (j == 0) {
        ans = (ans + dp[i][j]) % mod;
      } else {
        if (w[j - 1] > W - i) {
          ans = (ans + dp[i][j]) % mod;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p01450 (C++)

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

#define MOD 1000000007
#define INF 1000000007
typedef /*long long*/ int LLI;

int n;
int W;
int myws[201];
int imos[201];
LLI dp[2][1145][201];

int main() {
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &myws[i]);
  }
  sort(myws + 1, myws + n + 1);

  int src = 0;
  int dst = 1;
  dp[src][0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k < i; k++) {
      for (int j = 0; j <= W; j++) {
        dp[dst][j][k] = dp[src][j][k];
      }
    }

    for (int j = 0; j <= W; j++) {
      dp[dst][j][i] = dp[src][j][0];
    }

    int w = myws[i];
    for (int j = 0; j <= W - w; j++) {
      for (int k = 0; k < i; k++) {
        dp[dst][j + w][k] = (dp[dst][j + w][k] + dp[src][j][k]) % MOD;
      }
    }

    swap(src, dst);
    fill(dp[dst][0], dp[dst][W + 1], 0);
  }

  /*for (int i=0; i<=n; i++) {
    printf("%d:", i);
    for (int j=0; j<=W; j++) {
      int sum = 0;
      for (int k=0; k<=n; k++) sum += dp[i][j][k];
      printf(" %d", sum);
    }
    puts("");
  }*/

  LLI ans = 0;
  for (int k = 0; k <= n; k++) {
    int st = 0;
    if (k != 0)
      st = max(W - myws[k] + 1, 0);
    for (int j = st; j <= W; j++) {
      ans = (ans + dp[src][j][k]) % MOD;
    }
  }
  printf("%d\n", ans % MOD);
}

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

#define MOD 1000000007
#define INF 1000000007
typedef /*long long*/ int LLI;

int n;
int W;
int myws[201];
int imos[201];
LLI dp[2][11451][201];

int main() {
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &myws[i]);
  }
  sort(myws + 1, myws + n + 1);

  int src = 0;
  int dst = 1;
  dp[src][0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k < i; k++) {
      for (int j = 0; j <= W; j++) {
        dp[dst][j][k] = dp[src][j][k];
      }
    }

    for (int j = 0; j <= W; j++) {
      dp[dst][j][i] = dp[src][j][0];
    }

    int w = myws[i];
    for (int j = 0; j <= W - w; j++) {
      for (int k = 0; k < i; k++) {
        dp[dst][j + w][k] = (dp[dst][j + w][k] + dp[src][j][k]) % MOD;
      }
    }

    swap(src, dst);
    fill(dp[dst][0], dp[dst][W + 1], 0);
  }

  /*for (int i=0; i<=n; i++) {
    printf("%d:", i);
    for (int j=0; j<=W; j++) {
      int sum = 0;
      for (int k=0; k<=n; k++) sum += dp[i][j][k];
      printf(" %d", sum);
    }
    puts("");
  }*/

  LLI ans = 0;
  for (int k = 0; k <= n; k++) {
    int st = 0;
    if (k != 0)
      st = max(W - myws[k] + 1, 0);
    for (int j = st; j <= W; j++) {
      ans = (ans + dp[src][j][k]) % MOD;
    }
  }
  printf("%d\n", ans % MOD);
}
Problem p01450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int w[400], n, W, dp[20005];
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  sort(w, w + n);
  int sum = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W; j++)
      dp[j] = 0;
    dp[cnt] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int k = W - w[j]; k >= cnt; k--) {
        dp[k + w[j]] += dp[k];
        dp[k + w[j]] %= 1000000007;
      }
    }
    for (int j = W - w[i] + 1; j <= W; j++) {
      sum += dp[j];
      sum %= 1000000007;
    }
    cnt += w[i];
  }
  if (cnt <= W)
    sum++;
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int w[400], n, W, dp[20005];
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  sort(w, w + n);
  int sum = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W; j++)
      dp[j] = 0;
    dp[cnt] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int k = W - w[j]; k >= cnt; k--) {
        dp[k + w[j]] += dp[k];
        dp[k + w[j]] %= 1000000007;
      }
    }
    for (int j = W - w[i] + 1; j <= W; j++) {
      sum += dp[j];
      sum %= 1000000007;
    }
    cnt += w[i];
    if (cnt > W)
      break;
  }
  if (cnt <= W)
    sum++;
  cout << sum << endl;
  return 0;
}
Problem p01452 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const long long int MOD = 1000000007;
const int INF = 1000000000;

ll n, m, k;
ll kai[300000];

ll mod_pow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    y /= 2;
  }
  return ret;
}

ll conb(ll x, ll y) {
  ll ret = kai[x + y];
  ret = ret * mod_pow(kai[x], MOD - 2) % MOD;
  ret = ret * mod_pow(kai[y], MOD - 2) % MOD;
  return ret;
}

void init() {
  kai[0] = 1;
  for (ll i = 1; i <= 300000; i++) {
    kai[i] = (kai[i - 1] * i) % MOD;
  }
}

ll pre(ll x, ll y) {
  ll ret = 1;
  return ret;
}

int main() {
  cin >> n >> m >> k;
  init();
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll j = k - i;
    ll add = conb(n + 2 * i, m + 2 * j);
    ll add2 = conb(n + i, i);
    if (i > 0)
      add2 = (add2 - conb(n + i + 1, i - 1) + MOD) % MOD;
    ll add3 = conb(m + j, j);
    if (j > 0)
      add3 = (add3 - conb(m + j + 1, j - 1) + MOD) % MOD;
    add = add * add2 % MOD;
    add = add * add3 % MOD;
    ans = (ans + add) % MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const long long int MOD = 1000000007;
const int INF = 1000000000;

ll n, m, k;
ll kai[300000];

ll mod_pow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    y /= 2;
  }
  return ret;
}

ll conb(ll x, ll y) {
  ll ret = kai[x + y];
  ret = ret * mod_pow(kai[x], MOD - 2) % MOD;
  ret = ret * mod_pow(kai[y], MOD - 2) % MOD;
  return ret;
}

void init() {
  kai[0] = 1;
  for (ll i = 1; i < 300000; i++) {
    kai[i] = (kai[i - 1] * i) % MOD;
  }
}

ll pre(ll x, ll y) {
  ll ret = 1;
  return ret;
}

int main() {
  cin >> n >> m >> k;
  init();
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll j = k - i;
    ll add = conb(n + 2 * i, m + 2 * j);
    ll add2 = conb(n + i, i);
    if (i > 0)
      add2 = (add2 - conb(n + i + 1, i - 1) + MOD) % MOD;
    ll add3 = conb(m + j, j);
    if (j > 0)
      add3 = (add3 - conb(m + j + 1, j - 1) + MOD) % MOD;
    add = add * add2 % MOD;
    add = add * add3 % MOD;
    ans = (ans + add) % MOD;
  }
  cout << ans << endl;
}
Problem p01470 (C++)

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

typedef long long ll;

ll mod = 8589934609ll;

ll mul(ll x, ll y) { // x * y
  ll res = (x * (y >> 30)) % mod;
  for (int i = 0; i < 30; ++i)
    res = (res << 1) % mod;
  res += x * (((y >> 30) << 30) ^ y);
  res %= mod;
  return res;
}

ll powmod(ll x, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return x;

  if (p % 2)
    return mul(x, powmod(x, p - 1));
  else
    return powmod(mul(x, x), p / 2);
}

ll modInverse(ll x) { // x^(mod-2)
  return powmod(x, mod - 2);
}

int main() {
  int n, o, y;
  ll ans = 0;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> o >> y;

    switch (o) {
    case 1:
      ans += y;
      ans %= mod;
      break;
    case 2:
      ans -= y;
      ans = (ans + mod) % mod;
      break;
    case 3:
      ans = mul(ans, y);
      break;
    case 4:
      ans = mul(ans, modInverse(y));
      break;
    }
  }

  if (ans >= 1ll << 31)
    ans -= mod;
  cout << ans << endl;
}

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

typedef long long ll;

ll mod = 8589934609ll;

ll mul(ll x, ll y) { // x * y
  ll res = (x * (y >> 30)) % mod;

  res = (res << 15) % mod;
  res = (res << 15) % mod;
  res += x * (((y >> 30) << 30) ^ y);
  res %= mod;
  return res;
}

ll powmod(ll x, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return x;

  if (p % 2)
    return mul(x, powmod(x, p - 1));
  else
    return powmod(mul(x, x), p / 2);
}

ll modInverse(ll x) { // x^(mod-2)
  return powmod(x, mod - 2);
}

int main() {
  int n, o, y;
  ll ans = 0;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> o >> y;

    switch (o) {
    case 1:
      ans += y;
      ans %= mod;
      break;
    case 2:
      ans -= y;
      ans = (ans + mod) % mod;
      break;
    case 3:
      ans = mul(ans, y);
      break;
    case 4:
      ans = mul(ans, modInverse(y));
      break;
    }
  }

  if (ans >= 1ll << 31)
    ans -= mod;
  cout << ans << endl;
}
Problem p01471 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
struct node {
  double w, v, u;
};
vector<node> list;
bool cmp(node A, node B) { return A.v / A.w > B.v / B.w; }
int main() {
  // freopen("input.txt","r",stdin);
  int N;
  double W;
  while (scanf("%d%lf", &N, &W) == 2) {
    list.clear();
    double ans = 0.0;
    for (int i = 0; i < N; i++) {
      node obj;
      scanf("%lf%lf", &obj.w, &obj.v);
      obj.u = obj.v / obj.w;
      if (obj.w <= 0 && obj.v >= 0) {
        W -= obj.w;
        ans += obj.v;
        continue;
      }
      if (obj.w >= 0 && obj.v <= 0)
        continue;
      if (obj.w <= 0 && obj.v <= 0) {
        W -= obj.w;
        ans += obj.v;
        obj.w *= -1.0;
        obj.v *= -1.0;
      }
      list.push_back(obj);
    }
    sort(list.begin(), list.end(), cmp);
    for (int i = 0; i < N; i++) {
      if (list[i].w <= W) {
        W -= list[i].w;
        ans += list[i].v;
      } else {
        ans += list[i].u * W;
        W = 0.0;
      }
    }
    printf("%.8lf\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
struct node {
  double w, v, u;
};
vector<node> list;
bool cmp(node A, node B) { return A.v / A.w > B.v / B.w; }
int main() {
  // freopen("input.txt","r",stdin);
  int N;
  double W;
  while (scanf("%d%lf", &N, &W) == 2) {
    list.clear();
    double ans = 0.0;
    for (int i = 0; i < N; i++) {
      node obj;
      scanf("%lf%lf", &obj.w, &obj.v);
      obj.u = obj.v / obj.w;
      if (obj.w <= 0 && obj.v >= 0) {
        W -= obj.w;
        ans += obj.v;
        continue;
      }
      if (obj.w >= 0 && obj.v <= 0)
        continue;
      if (obj.w <= 0 && obj.v <= 0) {
        W -= obj.w;
        ans += obj.v;
        obj.w *= -1.0;
        obj.v *= -1.0;
      }
      list.push_back(obj);
    }
    sort(list.begin(), list.end(), cmp);
    for (int i = 0; i < list.size(); i++) {
      if (list[i].w <= W) {
        W -= list[i].w;
        ans += list[i].v;
      } else {
        ans += list[i].u * W;
        W = 0.0;
      }
    }
    printf("%.8lf\n", ans);
  }
  return 0;
}
Problem p01473 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <string>
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;
typedef long long ll;
string s;

ll fact(ll x) {
  if (x == 1)
    return 1;
  return x * fact(x - 1);
}

int main() {
  cin >> s;
  map<char, int> mp;
  rep(i, 0, s.size()) mp[s[i]]++;

  int cnt = 0;
  int sum = 0;
  ll ans;
  map<char, int>::iterator itr = mp.begin();
  while (itr != mp.end()) {
    // cout << itr->first << " "<< itr->second << endl;
    if (itr->second % 2 == 1) {
      (itr->second)--;
      cnt++;
    }
    sum += itr->second / 2;
    itr++;
  }
  if (s.size() == 1) {
    cout << 1 << endl;
  } else if (cnt > 0 && s.size() % 2 == 0)
    cout << 0 << endl;
  else if (cnt > 1)
    cout << 0 << endl;
  else {
    ans = fact(sum);
    itr = mp.begin();
    while (itr != mp.end()) {
      // cout << itr->first << " A "<< itr->second << endl;
      ans /= fact(itr->second / 2);
      itr++;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
#include <map>
#include <string>
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;
typedef long long ll;
string s;

ll fact(ll x) {
  if (x == 0)
    return 1;
  return x * fact(x - 1);
}

int main() {
  cin >> s;
  map<char, int> mp;
  rep(i, 0, s.size()) mp[s[i]]++;

  int cnt = 0;
  int sum = 0;
  ll ans;
  map<char, int>::iterator itr = mp.begin();
  while (itr != mp.end()) {
    // cout << itr->first << " "<< itr->second << endl;
    if (itr->second % 2 == 1) {
      (itr->second)--;
      cnt++;
    }
    sum += itr->second / 2;
    itr++;
  }
  if (s.size() == 1) {
    cout << 1 << endl;
  } else if (cnt > 0 && s.size() % 2 == 0)
    cout << 0 << endl;
  else if (cnt > 1)
    cout << 0 << endl;
  else {
    ans = fact(sum);
    itr = mp.begin();
    while (itr != mp.end()) {
      // cout << itr->first << " A "<< itr->second << endl;
      ans /= fact(itr->second / 2);
      itr++;
    }
    cout << ans << endl;
  }
}
Problem p01478 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <queue>
using namespace std;
int dp[1 << 24], x[8][8];
queue<int> Q;
int main() {
  int n;
  cin >> n;
  int minp = 0, maxp = 0;
  for (int i = 0; i < n; i++) {
    minp += (n - 1 - i) * (1 << (3 * i));
    maxp += i * (1 << (3 * i));
  }
  for (int i = minp; i <= maxp; i++)
    dp[i] = 1000000007;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> x[i][j];
  }
  int G = 0, maxn = 0;
  for (int i = 0; i < n; i++) {
    G += (1 << (3 * i)) * i;
  }
  Q.push(G);
  dp[G] = 0;
  while (!Q.empty()) {
    int H = Q.front();
    Q.pop();
    int I[8], J[8];
    for (int i = 0; i < 8; i++)
      I[i] = (H / (1 << (3 * i))) % 8;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < 8; k++)
          J[k] = I[k];
        swap(J[i], J[j]);
        int K = 0;
        for (int k = 0; k < 8; k++)
          K += (1 << (3 * k)) * J[k];
        if (dp[K] > dp[H] + x[i][j]) {
          dp[K] = dp[H] + x[i][j];
          Q.push(K);
        }
      }
    }
  }
  for (int i = minp; i <= maxp; i++) {
    if (dp[i] != 1000000007 && maxn < dp[i])
      maxn = dp[i];
  }
  cout << maxn << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
using namespace std;
int dp[16434825], x[8][8];
queue<int> Q;
int main() {
  int n;
  cin >> n;
  int minp = 0, maxp = 0;
  for (int i = 0; i < n; i++) {
    minp += (n - 1 - i) * (1 << (3 * i));
    maxp += i * (1 << (3 * i));
  }
  for (int i = minp; i <= maxp; i++)
    dp[i] = 1000000007;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> x[i][j];
  }
  int G = 0, maxn = 0;
  for (int i = 0; i < n; i++) {
    G += (1 << (3 * i)) * i;
  }
  Q.push(G);
  dp[G] = 0;
  while (!Q.empty()) {
    int H = Q.front();
    Q.pop();
    int I[8], J[8];
    for (int i = 0; i < 8; i++)
      I[i] = (H / (1 << (3 * i))) % 8;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < 8; k++)
          J[k] = I[k];
        swap(J[i], J[j]);
        int K = 0;
        for (int k = 0; k < 8; k++)
          K += (1 << (3 * k)) * J[k];
        if (dp[K] > dp[H] + x[i][j]) {
          dp[K] = dp[H] + x[i][j];
          Q.push(K);
        }
      }
    }
  }
  for (int i = minp; i <= maxp; i++) {
    if (dp[i] != 1000000007 && maxn < dp[i])
      maxn = dp[i];
  }
  cout << maxn << endl;
  return 0;
}
Problem p01478 (C++)

Buggy submission (status: Memory Limit Exceeded):
// 33
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct S {
  int t;
  vector<int> v;
  bool operator<(S a) const { return t > a.t; }
};

int fact(int x) { return (x == 1) ? 1 : x * fact(x - 1); }

int dec(vector<int> v) {
  vector<int> n(v.size());
  for (int i = 0; i < n.size(); i++) {
    n[i] = i;
  }
  int r = 0;
  for (int i = 0; i < v.size(); i++) {
    r *= n.size();
    vector<int>::iterator it = find(n.begin(), n.end(), v[i]);
    r += it - n.begin();
    n.erase(it);
  }
  return r;
}

int main() {
  int n;
  cin >> n;
  int cs[8][8];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cs[i][j];
    }
  }
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = i;
  }
  bool p[50000] = {};
  int r = fact(n);
  S is = {0, v};
  priority_queue<S> que;
  que.push(is);
  int lt;
  while (r) {
    S c = que.top();
    que.pop();
    if (p[dec(c.v)]++)
      continue;
    r--;
    lt = c.t;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        S n = c;
        n.t += cs[i][j];
        swap(n.v[i], n.v[j]);
        que.push(n);
      }
    }
  }
  cout << lt << endl;
  return 0;
}

Fixed submission:
// 33
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct S {
  int t;
  vector<int> v;
  bool operator<(S a) const { return t > a.t; }
};

int fact(int x) { return (x == 1) ? 1 : x * fact(x - 1); }

int dec(vector<int> v) {
  vector<int> n(v.size());
  for (int i = 0; i < n.size(); i++) {
    n[i] = i;
  }
  int r = 0;
  for (int i = 0; i < v.size(); i++) {
    r *= n.size();
    vector<int>::iterator it = find(n.begin(), n.end(), v[i]);
    r += it - n.begin();
    n.erase(it);
  }
  return r;
}

int main() {
  int n;
  cin >> n;
  int cs[8][8];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cs[i][j];
    }
  }
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = i;
  }
  bool p[50000] = {};
  int r = fact(n);
  S is = {0, v};
  priority_queue<S> que;
  que.push(is);
  int lt;
  while (r) {
    S c = que.top();
    que.pop();
    if (p[dec(c.v)]++)
      continue;
    r--;
    lt = c.t;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        S n = c;
        n.t += cs[i][j];
        swap(n.v[i], n.v[j]);
        if (!p[dec(n.v)])
          que.push(n);
      }
    }
  }
  cout << lt << endl;
  return 0;
}
Problem p01478 (C++)

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

using namespace std;

int cost[9][9];

struct node {
  string num;
  int cost;
  node(string n, int c) : num(n), cost(c) {}
  node() {}
  bool operator>(const node &arg) const { return cost > arg.cost; }
};

int main() {
  int n;
  string str = "";
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cost[i][j];
    }
  }

  for (int i = 0; i < n; i++)
    str += (char)('1' + i);

  priority_queue<node, vector<node>, greater<node>> q;
  map<string, bool> mb;
  map<string, int> mi;
  string tmpstr, working;
  int tmpcost;
  node tmpnode;
  q.push(node(str, 0));
  while (!q.empty()) {
    tmpnode = q.top();
    q.pop();
    tmpstr = tmpnode.num;
    tmpcost = tmpnode.cost;

    if (mb[tmpstr] == true)
      continue;
    mb[tmpstr] = true;
    mi[tmpstr] = tmpcost;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        working = tmpstr;
        swap(working[i], working[j]);
        q.push(
            node(working, tmpcost + cost[working[i] - '1'][working[j] - '1']));
      }
    }
  }
  map<string, int>::iterator it = mi.begin();
  int ret = -1;
  while (it != mi.end()) {
    ret = max(ret, (*it).second);
    ++it;
  }
  cout << ret << endl;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int cost[9][9];

struct node {
  string num;
  int cost;
  node(string n, int c) : num(n), cost(c) {}
  node() {}
  bool operator>(const node &arg) const { return cost > arg.cost; }
};

int main() {
  int n;
  string str = "";
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> cost[i][j];
    }
  }

  for (int i = 0; i < n; i++)
    str += (char)('1' + i);

  priority_queue<node, vector<node>, greater<node>> q;
  map<string, bool> mb;
  map<string, int> mi;
  string tmpstr, working;
  int tmpcost;
  node tmpnode;
  q.push(node(str, 0));
  while (!q.empty()) {
    tmpnode = q.top();
    q.pop();
    tmpstr = tmpnode.num;
    tmpcost = tmpnode.cost;

    if (mb[tmpstr] == true)
      continue;
    mb[tmpstr] = true;
    mi[tmpstr] = tmpcost;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        working = tmpstr;
        swap(working[i], working[j]);
        if (mb[working] == false) {
          q.push(node(working,
                      tmpcost + cost[working[i] - '1'][working[j] - '1']));
        }
      }
    }
  }
  map<string, int>::iterator it = mi.begin();
  int ret = -1;
  while (it != mi.end()) {
    ret = max(ret, (*it).second);
    ++it;
  }
  cout << ret << endl;
}
Problem p01479 (C++)

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

// 2362
int main() {
  string s;
  cin >> s;
  string ec[2] = {"egg", "chicken"};
  vii v;
  vi u;
  int last = -1;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 2; j++) {
      if (s[i] == ec[j][0]) {
        if (j == last) {
          v.pb(u);
          u.clear();
          u.pb(j);
        } else {
          u.pb(j);
          last = j;
        }
        i += ec[j].size() - 1;
      }
    }
  }

  int m = 0;
  for (int i = 1; i < v.size(); i++) {
    if (v[m].size() < v[i].size())
      m = i;
  }

  cout << ec[v[m][v[m].size() - 1]] << endl;
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

// 2362
int main() {
  string s;
  cin >> s;
  string ec[2] = {"egg", "chicken"};
  vii v;
  vi u;
  int last = -1;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 0; j < 2; j++) {
      if (s[i] == ec[j][0]) {
        if (j == last) {
          v.pb(u);
          u.clear();
          u.pb(j);
        } else {
          u.pb(j);
          last = j;
        }
        i += ec[j].size() - 1;
      }
    }
  }
  v.pb(u);
  int m = 0;
  for (int i = 1; i < v.size(); i++) {
    if (v[m].size() < v[i].size())
      m = i;
  }

  cout << ec[v[m][v[m].size() - 1]] << endl;
}
Problem p01479 (C++)

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

using namespace std;

int main() {
  char s[1001];
  cin >> s;
  char p = 0;
  int l = 0;
  char r;
  int rl = -1;
  for (int i = 0; s[i]; i += (p == 'e') ? 3 : 7) {
    if (p == s[i]) {
      l = 0;
    }
    l++;
    p = s[i];
    if (l > rl) {
      rl = l;
      r = p;
    }
  }
  cout << ((r == 'e') ? "egg" : "chicken") << endl;
  return 0;
}

Fixed submission:
// 59
#include <iostream>

using namespace std;

int main() {
  char s[7 * 1001];
  cin >> s;
  char p = 0;
  int l = 0;
  char r;
  int rl = -1;
  for (int i = 0; s[i]; i += (p == 'e') ? 3 : 7) {
    if (p == s[i]) {
      l = 0;
    }
    l++;
    p = s[i];
    if (l > rl) {
      rl = l;
      r = p;
    }
  }
  cout << ((r == 'e') ? "egg" : "chicken") << endl;
  return 0;
}
Problem p01479 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <vector>
using namespace std;
struct S {
  int length, ancestor;
  bool operator>(const S &s) const { return length > s.length; }
};

vector<S> v;
S s;
void F(int id) {
  if (s.ancestor == id) {
    v.push_back(s);
    s.length = 1;
    s.ancestor = id;
  } else {
    ++s.length;
    s.ancestor = id;
  }
}
int main() {
  char str[1024];
  scanf("%s", str);
  s.length = 0;
  s.ancestor = -1;
  for (int i = 0; str[i];) {
    if (str[i] == 'e') {
      F(0);
      i += 3;
    } else {
      F(1);
      i += 7;
    }
  }
  if (s.ancestor >= 0)
    v.push_back(s);
  stable_sort(v.begin(), v.end(), greater<S>());
  puts(v[0].ancestor ? "chicken" : "egg");
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <vector>
using namespace std;
struct S {
  int length, ancestor;
  bool operator>(const S &s) const { return length > s.length; }
};

vector<S> v;
S s;
void F(int id) {
  if (s.ancestor == id) {
    v.push_back(s);
    s.length = 1;
    s.ancestor = id;
  } else {
    ++s.length;
    s.ancestor = id;
  }
}
int main() {
  char str[8192];
  scanf("%s", str);
  s.length = 0;
  s.ancestor = -1;
  for (int i = 0; str[i];) {
    if (str[i] == 'e') {
      F(0);
      i += 3;
    } else {
      F(1);
      i += 7;
    }
  }
  if (s.ancestor >= 0)
    v.push_back(s);
  stable_sort(v.begin(), v.end(), greater<S>());
  puts(v[0].ancestor ? "chicken" : "egg");
}
Problem p01486 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
bool iscat(string s) {
  int l = s.length();
  if (l == 0 || s == "mew")
    return true;
  if (s[0] != 'm' || s[l - 1] != 'w' || l % 3 != 0)
    return false;
  int id = 1;
  int n;
  while ((n = s.find('e', id)) != string::npos) {
    string s1 = s.substr(1, n - 1);
    string s2 = s.substr(n + 1, l - n - 2);
    if (iscat(s1) && iscat(s2))
      return true;
    id = n + 1;
  }
  return false;
}
int main() {
  string s;
  cin >> s;
  if (iscat(s))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
bool iscat(string s) {
  int l = s.length();
  if (l == 0 || s == "mew")
    return true;
  if (s[0] != 'm' || s[l - 1] != 'w' || l % 3 != 0)
    return false;
  int m = 0, e = 0, w = 0;
  for (int i = 0; i < l; i++) {
    switch (s[i]) {
    case 'm':
      m++;
      break;
    case 'e':
      e++;
      break;
    case 'w':
      w++;
      break;
    }
  }
  if (m != e || e != m)
    return false;
  int id = 1;
  int n;
  while ((n = s.find('e', id)) != string::npos) {
    string s1 = s.substr(1, n - 1);
    string s2 = s.substr(n + 1, l - n - 2);
    if (iscat(s1) && iscat(s2))
      return true;
    id = n + 1;
  }
  return false;
}
int main() {
  string s;
  cin >> s;
  if (iscat(s))
    cout << "Cat" << endl;
  else
    cout << "Rabbit" << endl;
  return 0;
}