prithivMLmods/Coder-Stat huggingface dataset:
Problem p00276 (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 <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[32][32][32];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 8 && a > 4 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 8 && a > 4) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 8) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\n", res);
  }
  return 0;
}

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[64][32][32];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 8 && a > 4 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 8 && a > 4) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 8) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\n", res);
  }
  return 0;
}
Problem p00276 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int A[12];
int main() {
  int i, a, b, c = 0, d = 0, n, e = 0, f = 0, l = -1, j;
  char S[102];
  scanf("%d", &d);
  for (i = 0; i < d; i++) {
    scanf("%d %d %d", &c, &a, &n);
    while (1) {
      if (c > 0 && a > 0 && n > 0) {
        e++;
        c--;
        a--;
        n--;
      } else if (c > 1 && a > 0) {
        c = c - 2;
        a--;
        e++;
      } else if (c > 2) {
        c = c - 3;
        e++;
      }
      if (a == 0 && n == 0 && c < 3 || c == 0)
        break;
    }
    printf("%d\n", e);
    e = 0;
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int A[12];
int main() {
  int i, a, b, c = 0, d = 0, n, e = 0, f = 0, l = -1, j;
  char S[102];
  scanf("%d", &d);
  for (i = 0; i < d; i++) {
    scanf("%d %d %d", &c, &a, &n);
    while (1) {
      if (c > 0 && a > 0 && n > 0) {
        e++;
        c--;
        a--;
        n--;
      } else if (c > 1 && a > 0) {
        c = c - 2;
        a--;
        e++;
      } else if (c > 2) {
        c = c - 3;
        e++;
      } else
        break;
    }
    printf("%d\n", e);
    e = 0;
  }
  return 0;
}
Problem p00276 (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 <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[16][16][16];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 10 && a > 5 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 10 && a > 5) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 10) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\n", res);
  }
  return 0;
}

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[128][128][128];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 10 && a > 5 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 10 && a > 5) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 10) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\n", res);
  }
  return 0;
}
Problem p00276 (C++)

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

int main() {
  int Q;
  cin >> Q;
  while (Q--) {
    int C, A, N;
    cin >> C >> A >> N;
    int ans = 0;
    for (int k = 0; k <= N; k++) {
      for (int i = 0; i <= A; i++) {
        for (int j = 0; j <= C / 3; j++) {
          int c = i * 2 + j * 3 + k;
          int a = i + k;
          int n = k;
          if (c <= C && a <= A && n <= N)
            ans = max(ans, i + j + k);
        }
      }
    }
    cout << ans << endl;
  }
}

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

int main() {
  int Q;
  cin >> Q;
  while (Q--) {
    int C, A, N;
    cin >> C >> A >> N;
    int ans = 0;
    for (int k = 0; k <= N; k++) {
      for (int i = 0; i <= (A - k); i++) {
        for (int j = 0; j <= (C - k - 2 * i) / 3; j++) {
          int c = i * 2 + j * 3 + k;
          int a = i + k;
          int n = k;
          if (c <= C && a <= A && n <= N)
            ans = max(ans, i + j + k);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00276 (C++)

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

using namespace std;

int q;
int c, a, n;

int main(void) {
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &c, &a, &n);
    int res = 0;
    for (int j = 0; j <= c / 3; j++) {
      for (int k = 0; k <= c / 2; k++) {
        for (int l = 0; l <= min(c, min(a, n)); l++) {
          int use_c = j * 3 + k * 2 + l;
          int use_a = k + l;
          int use_n = l;
          if (use_c <= c && use_a <= a && use_n <= n)
            res = max(res, j + k + l);
        }
      }
    }
    printf("%d\n", res);
  }
  return 0;
}

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

using namespace std;

int q;
int c, a, n;

int main(void) {
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &c, &a, &n);
    int res = 0;
    while (c > 0 && a > 0 && n > 0) {
      c--;
      a--;
      n--;
      res++;
    }
    while (c > 1 && a > 0) {
      c -= 2;
      a--;
      res++;
    }
    while (c > 2) {
      c -= 3;
      res++;
    }
    printf("%d\n", res);
  }
  return 0;
}
Problem p00276 (C++)

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

using namespace std;

int main() {
  int Q;
  cin >> Q;
  while (Q-- > 0) {
    int c, a, n, ans = 0;
    cin >> c >> a >> n;
    // 1 1 1
    for (int i = 0; i <= n; i++) {
      // 2 1 0
      for (int j = 0; j <= (a - i); j++) {
        // 3 0 0
        for (int k = 0; k <= (c - i - 2 * j); k++) {
          int c_ = i + 2 * j + 3 * k, a_ = i + j, n_ = i;
          if (c_ <= c && a_ <= a && n_ <= n)
            ans = max(ans, i + j + k);
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

int main() {
  int Q;
  cin >> Q;
  while (Q-- > 0) {
    int c, a, n, ans = 0;
    cin >> c >> a >> n;
    // 1 1 1
    for (int i = 0; i <= n; i++) {
      // 2 1 0
      for (int j = 0; j <= (a - i); j++) {
        // 3 0 0
        for (int k = 0; k <= (c - i - 2 * j) / 3; k++) {
          int c_ = i + 2 * j + 3 * k, a_ = i + j, n_ = i;
          if (c_ <= c && a_ <= a && n_ <= n)
            ans = max(ans, i + j + k);
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00277 (C++)

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

typedef std::pair<int, int> P;

const int MAX_N = 100000;

int n;
// fst: -得点, snd: チームの番号
P dat[MAX_N * 2 - 1];

int cnt[MAX_N];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;

  for (int i = 0; i != n * 2 - 1; ++i)
    dat[i] = std::make_pair(INT_MAX, INT_MAX);

  return;
}

void update(int k, P x) {
  k += n - 1;
  dat[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = std::min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }

  return;
}

// [a,b)
P query(int a, int b, int k, int l, int r) {
  if (a >= r || b <= l)
    return std::make_pair(INT_MAX, INT_MAX);

  if (a <= l && r <= b)
    return dat[k];
  else {
    P vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    P vr = query(a, b, k * 2 + 2, (l + r) / 2, r);

    return std::min(vl, vr);
  }
}

int main() {
  int N, R, L;
  std::cin >> N >> R >> L;

  init(N);
  for (int i = 0; i != N; ++i)
    update(i, std::make_pair(0, i));

  int prevT = 0;
  for (int i = 0; i != R; ++i) {
    int d, t, x;
    std::cin >> d >> t >> x;

    --d;

    if (t != prevT) {
      P p = query(0, N, 0, 0, n);
      cnt[p.second] += t - prevT;
    }

    P p = query(d, d + 1, 0, 0, n);
    update(d, std::make_pair(p.first - x, p.second));

    prevT = t;
  }

  if (prevT != L) {
    P p = query(0, N, 0, 0, n);
    cnt[p.second] += L - prevT;
  }

  int t = -1, id;
  for (int i = 0; i != N; ++i) {
    if (t < cnt[i]) {
      t = cnt[i];
      id = i + 1;
    }
  }

  std::cout << id << std::endl;

  return 0;
}

Fixed submission:
#include <climits>
#include <iostream>
#include <utility>

typedef std::pair<int, int> P;

const int MAX_N = 1 << 17;

int n;
// fst: -得点, snd: チームの番号
P dat[MAX_N * 2 - 1];

int cnt[MAX_N];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;

  for (int i = 0; i != n * 2 - 1; ++i)
    dat[i] = std::make_pair(INT_MAX, INT_MAX);

  return;
}

void update(int k, P x) {
  k += n - 1;
  dat[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = std::min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }

  return;
}

// [a,b)
P query(int a, int b, int k, int l, int r) {
  if (a >= r || b <= l)
    return std::make_pair(INT_MAX, INT_MAX);

  if (a <= l && r <= b)
    return dat[k];
  else {
    P vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    P vr = query(a, b, k * 2 + 2, (l + r) / 2, r);

    return std::min(vl, vr);
  }
}

int main() {
  int N, R, L;
  std::cin >> N >> R >> L;

  init(N);
  for (int i = 0; i != N; ++i)
    update(i, std::make_pair(0, i));

  int prevT = 0;
  for (int i = 0; i != R; ++i) {
    int d, t, x;
    std::cin >> d >> t >> x;

    --d;

    if (t != prevT) {
      P p = query(0, N, 0, 0, n);
      cnt[p.second] += t - prevT;
    }

    P p = query(d, d + 1, 0, 0, n);
    update(d, std::make_pair(p.first - x, p.second));

    prevT = t;
  }

  if (prevT != L) {
    P p = query(0, N, 0, 0, n);
    cnt[p.second] += L - prevT;
  }

  int t = -1, id;
  for (int i = 0; i != N; ++i) {
    if (t < cnt[i]) {
      t = cnt[i];
      id = i + 1;
    }
  }

  std::cout << id << std::endl;

  return 0;
}
Problem p00277 (C++)

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

#define INF 2000000000
typedef pair<int, int> seg;
seg dat[1 << 18];
int ans[1 << 17];

int n, N, R, L;

void update(int i, seg x) {
  i += n - 1;
  dat[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
}

void init() {
  n = 1;
  while (n < N)
    n *= 2;
  n *= 2;
  for (int i = 0; i < n * 2 - 1; i++)
    dat[i] = make_pair(INF, INF);
  for (int i = 0; i < N; i++)
    update(i, make_pair(0, i));
}

seg query(int a, int b, int k, int l, int r) {
  if (a >= r || b <= l)
    return make_pair(INF, INF);
  if (a <= l && b >= r)
    return dat[k];
  seg vr = query(a, b, k, (l + r) / 2, r);
  seg vl = query(a, b, k, l, (l + r) / 2);
  return min(vr, vl);
}

void output() {
  int m = 0;
  for (int i = 0; i < N; i++) {
    //    cout << ans[i] << " ";
    m = max(m, ans[i]);
  }
  for (int i = 0; i < N; i++) {
    if (ans[i] == m) {
      cout << i + 1 << endl;
      return;
    }
  }
}

int main() {
  int old_t = 0;

  cin >> N >> R >> L;
  init();
  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    d--;
    ans[query(0, N, 0, 0, n).second] += t - old_t;

    seg s = dat[d + n - 1];
    update(d, make_pair(s.first - x, s.second));
    old_t = t;
  }
  ans[query(0, N, 0, 0, n).second] += L - old_t;

  output();
}

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

#define INF 2000000000
typedef pair<int, int> seg;
seg dat[1 << 18 + 1];
int ans[1 << 17];

int n, N, R, L;

void update(int i, seg x) {
  i += n - 1;
  dat[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
}

void init() {
  n = 1;
  while (n < N)
    n *= 2;
  n *= 2;
  for (int i = 0; i < n * 2 - 1; i++)
    dat[i] = make_pair(INF, INF);
  for (int i = 0; i < N; i++)
    update(i, make_pair(0, i));
}

seg query(int a, int b, int k, int l, int r) {
  if (a >= r || b <= l)
    return make_pair(INF, INF);
  if (a <= l && b >= r)
    return dat[k];
  seg vr = query(a, b, k, (l + r) / 2, r);
  seg vl = query(a, b, k, l, (l + r) / 2);
  return min(vr, vl);
}

void output() {
  int m = 0;
  for (int i = 0; i < N; i++) {
    //    cout << ans[i] << " ";
    m = max(m, ans[i]);
  }
  for (int i = 0; i < N; i++) {
    if (ans[i] == m) {
      cout << i + 1 << endl;
      return;
    }
  }
}

int main() {
  int old_t = 0;

  cin >> N >> R >> L;
  init();
  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    d--;
    ans[query(0, N, 0, 0, n).second] += t - old_t;

    seg s = dat[d + n - 1];
    update(d, make_pair(s.first - x, s.second));
    old_t = t;
  }
  ans[query(0, N, 0, 0, n).second] += L - old_t;

  output();
}
Problem p00277 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define N_MAX 100000
typedef pair<int, int> P;

int main() {
  int N, R, L;
  int d, t, x;
  int time[N_MAX] = {0};
  int score[N_MAX] = {0};
  cin >> N >> R >> L;
  priority_queue<P> que;
  for (int i = 1; i <= N; i++) {
    que.push(P(0, N - i));
  }
  int now = 0;
  for (int j = 0; j < R; j++) {
    cin >> d >> t >> x;
    P p1 = que.top();
    while (score[N - p1.second] != p1.first) {
      que.pop();
      p1 = que.top();
    }
    time[N - p1.second] += t - now;
    now = t;
    score[d] += x;
    que.push(P(score[d], N - d));
  }
  P p1 = que.top();
  while (score[N - p1.second] != p1.first) {
    que.pop();
    p1 = que.top();
  }
  time[N - p1.second] += L - now;
  priority_queue<P> que2;
  for (int i = 1; i <= N; i++) {
    que2.push(P(time[i], N - i));
  }
  p1 = que2.top();
  cout << N - p1.second << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define N_MAX 100001
typedef pair<int, int> P;

int main() {
  int N, R, L;
  int d, t, x;
  int time[N_MAX] = {0};
  int score[N_MAX] = {0};
  cin >> N >> R >> L;
  priority_queue<P> que;
  for (int i = 1; i <= N; i++) {
    que.push(P(0, N - i));
  }
  int now = 0;
  for (int j = 0; j < R; j++) {
    cin >> d >> t >> x;
    P p1 = que.top();
    while (score[N - p1.second] != p1.first) {
      que.pop();
      p1 = que.top();
    }
    time[N - p1.second] += t - now;
    now = t;
    score[d] += x;
    que.push(P(score[d], N - d));
  }
  P p1 = que.top();
  while (score[N - p1.second] != p1.first) {
    que.pop();
    p1 = que.top();
  }
  time[N - p1.second] += L - now;
  priority_queue<P> que2;
  for (int i = 1; i <= N; i++) {
    que2.push(P(time[i], N - i));
  }
  p1 = que2.top();
  cout << N - p1.second << endl;
}
Problem p00277 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
#define MAX 1111111
#define MIN -1111111
typedef pair<int, int> P;
void init(int n);
void update(int k, int x);
P maxx(int k1, int k2);
P dat[2 * 100001];
int tim[100001] = {0};
int N;

int main() {
  int R, L;
  cin >> N >> R >> L;
  int n_tmp = N;
  init(N);
  memset(tim, 0, n_tmp + 1);
  int now = 0;
  dat[0] = P(MIN, 1);
  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    tim[dat[0].second] += t - now;
    update(d, x);
    now = t;
  }
  tim[dat[0].second] += L - now;
  int *ans = max_element(tim, tim + n_tmp + 1);
  cout << ans - tim << endl;
}

void init(int n) {
  N = 1;
  while (N < n)
    N *= 2;
  for (int i = 0; i < 2 * N - 1; i++) {
    dat[i] = P(MIN, MAX);
  }
  for (int i = N - 1; i < N - 1 + n; i++) {
    dat[i] = P(0, i - (N - 2));
  }
  return;
}

void update(int k, int x) {
  k += N - 2;
  P p = dat[k];
  int score = p.first;
  int team = p.second;
  // cout << score << " " << team << endl;
  dat[k] = P(score + x, team);
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = maxx(k * 2 + 1, k * 2 + 2);
    // cout << dat[k].first << " " << dat[k].second << endl;
  }
  return;
}

P maxx(int k1, int k2) {
  P p;
  if (dat[k1].first == dat[k2].first) {
    p = P(dat[k1].first, min(dat[k1].second, dat[k2].second));
  } else {
    p = max(dat[k1], dat[k2]);
  }
  return p;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
#define MAX 1111111
#define MIN -1111111
typedef pair<int, int> P;
void init(int n);
void update(int k, int x);
P maxx(int k1, int k2);
P dat[4 * 100001];
int tim[100001] = {0};
int N;

int main() {
  int R, L;
  cin >> N >> R >> L;
  int n_tmp = N;
  init(N);
  memset(tim, 0, n_tmp + 1);
  int now = 0;
  dat[0] = P(MIN, 1);
  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    tim[dat[0].second] += t - now;
    update(d, x);
    now = t;
  }
  tim[dat[0].second] += L - now;
  int *ans = max_element(tim, tim + n_tmp + 1);
  cout << ans - tim << endl;
}

void init(int n) {
  N = 1;
  while (N < n)
    N *= 2;
  for (int i = 0; i < 2 * N - 1; i++) {
    dat[i] = P(MIN, MAX);
  }
  for (int i = N - 1; i < N - 1 + n; i++) {
    dat[i] = P(0, i - (N - 2));
  }
  return;
}

void update(int k, int x) {
  k += N - 2;
  P p = dat[k];
  int score = p.first;
  int team = p.second;
  // cout << score << " " << team << endl;
  dat[k] = P(score + x, team);
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = maxx(k * 2 + 1, k * 2 + 2);
    // cout << dat[k].first << " " << dat[k].second << endl;
  }
  return;
}

P maxx(int k1, int k2) {
  P p;
  if (dat[k1].first == dat[k2].first) {
    p = P(dat[k1].first, min(dat[k1].second, dat[k2].second));
  } else {
    p = max(dat[k1], dat[k2]);
  }
  return p;
}
Problem p00277 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[100010];
struct node {
  int id, point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\n", 1 + (max_element(a, a + n) - a));
}

Fixed submission:
#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[150010];
struct node {
  int id, point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\n", 1 + (max_element(a, a + n) - a));
}
Problem p00277 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[125010];
struct node {
  int id, point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\n", 1 + (max_element(a, a + n) - a));
}

Fixed submission:
#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[137510];
struct node {
  int id, point;
} node[1 << 21];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\n", 1 + (max_element(a, a + n) - a));
}
Problem p00277 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[130010];
struct node {
  int id, point;
} node[1 << 20];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\n", 1 + (max_element(a, a + n) - a));
}

Fixed submission:
#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[135010];
struct node {
  int id, point;
} node[1 << 20];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\n", 1 + (max_element(a, a + n) - a));
}
Problem p00277 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[130625];
struct node {
  int id, point;
} node[1 << 18];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\n", 1 + (max_element(a, a + n) - a));
}

Fixed submission:
#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[131100];
struct node {
  int id, point;
} node[1 << 18];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\n", 1 + (max_element(a, a + n) - a));
}
Problem p00277 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int a[200005][2];
int s[100005];
int main() {
  int i, j, k;
  int n, r, l;
  int d = 0;
  int t = 0;
  int x = 0;
  int nn = 0;
  int f = 0;
  memset(a, 0, sizeof(a));
  memset(s, 0, sizeof(s));
  cin >> n >> r >> l;
  nn = 1;
  while (nn < n) {
    nn *= 2;
  }
  nn--;
  for (i = 0; i < n; i++) {
    a[nn + i][1] = i;
  }
  d = nn;
  while (d != 0) {
    d--;
    if (a[d * 2 + 1][0] < a[d * 2 + 2][0]) {
      a[d][0] = a[d * 2 + 2][0];
      a[d][1] = a[d * 2 + 2][1];
    } else {
      a[d][0] = a[d * 2 + 1][0];
      a[d][1] = a[d * 2 + 1][1];
    }
  }
  for (i = 0; i < r; i++) {
    cin >> d >> t >> x;
    s[f] += t;
    d--;
    d += nn;
    a[d][0] += x;
    while (d != 0) {
      d = (d - 1) / 2;
      if (a[d * 2 + 1][0] < a[d * 2 + 2][0]) {
        a[d][0] = a[d * 2 + 2][0];
        a[d][1] = a[d * 2 + 2][1];
      } else {
        a[d][0] = a[d * 2 + 1][0];
        a[d][1] = a[d * 2 + 1][1];
      }
    }
    f = a[0][1];
    s[f] -= t;
  }
  s[f] += l;
  nn = 0;
  for (i = 0; i < n; i++) {
    if (s[nn] < s[i])
      nn = i;
  }
  nn++;
  cout << nn << endl;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int a[400005][2];
int s[100005];
int main() {
  int i, j, k;
  int n, r, l;
  int d = 0;
  int t = 0;
  int x = 0;
  int nn = 0;
  int f = 0;
  memset(a, 0, sizeof(a));
  memset(s, 0, sizeof(s));
  cin >> n >> r >> l;
  nn = 1;
  while (nn < n) {
    nn *= 2;
  }
  nn--;
  for (i = 0; i < n; i++) {
    a[nn + i][1] = i;
  }
  d = nn;
  while (d != 0) {
    d--;
    if (a[d * 2 + 1][0] < a[d * 2 + 2][0]) {
      a[d][0] = a[d * 2 + 2][0];
      a[d][1] = a[d * 2 + 2][1];
    } else {
      a[d][0] = a[d * 2 + 1][0];
      a[d][1] = a[d * 2 + 1][1];
    }
  }
  for (i = 0; i < r; i++) {
    cin >> d >> t >> x;
    s[f] += t;
    d--;
    d += nn;
    a[d][0] += x;
    while (d != 0) {
      d = (d - 1) / 2;
      if (a[d * 2 + 1][0] < a[d * 2 + 2][0]) {
        a[d][0] = a[d * 2 + 2][0];
        a[d][1] = a[d * 2 + 2][1];
      } else {
        a[d][0] = a[d * 2 + 1][0];
        a[d][1] = a[d * 2 + 1][1];
      }
    }
    f = a[0][1];
    s[f] -= t;
  }
  s[f] += l;
  nn = 0;
  for (i = 0; i < n; i++) {
    if (s[nn] < s[i])
      nn = i;
  }
  nn++;
  cout << nn << endl;
}
Problem p00277 (C++)

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

using namespace std;

#define INF (1e9)
typedef pair<int, int> pii;
const int MAX_N = (1 << 17);

struct RMQ {
  int n;
  pii dat[MAX_N];
  void init(int n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = pii(-INF, -INF);
    }

    for (int i = 0; i < n_; i++) {
      update(i, 0);
    }
  }

  void update(int k, int a) {
    int nk = k;
    k += n - 1;
    dat[k] = pii(a, -nk);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  pii query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return pii(-INF, -INF);
    if (a <= l && r <= b) {
      return dat[k];
    } else {
      pii vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      pii vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

int main() {
  int N, R, L;
  cin >> N >> R >> L;
  int d, t, x, pt = 0;
  int point[MAX_N] = {};
  RMQ r;
  r.init(N);
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    d--;
    point[-r.query(0, N, 0, 0, r.n).second] += t - pt;
    r.update(d, x + r.dat[d + r.n - 1].first);
    pt = t;
  }
  point[-r.query(0, N, 0, 0, r.n).second] += L - pt;
  int mx = 0, num = -1;
  for (int i = 0; i < N; i++) {
    if (mx < point[i]) {
      mx = point[i];
      num = i + 1;
    }
  }
  cout << num << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define INF (1e9)
typedef pair<int, int> pii;
const int MAX_N = (1 << 18);

struct RMQ {
  int n;
  pii dat[MAX_N];
  void init(int n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = pii(-INF, -INF);
    }

    for (int i = 0; i < n_; i++) {
      update(i, 0);
    }
  }

  void update(int k, int a) {
    int nk = k;
    k += n - 1;
    dat[k] = pii(a, -nk);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  pii query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return pii(-INF, -INF);
    if (a <= l && r <= b) {
      return dat[k];
    } else {
      pii vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      pii vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

int main() {
  int N, R, L;
  cin >> N >> R >> L;
  int d, t, x, pt = 0;
  int point[MAX_N] = {};
  RMQ r;
  r.init(N);
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    d--;
    point[-r.query(0, N, 0, 0, r.n).second] += t - pt;
    r.update(d, x + r.dat[d + r.n - 1].first);
    pt = t;
  }
  point[-r.query(0, N, 0, 0, r.n).second] += L - pt;
  int mx = 0, num = -1;
  for (int i = 0; i < N; i++) {
    if (mx < point[i]) {
      mx = point[i];
      num = i + 1;
    }
  }
  cout << num << endl;
  return 0;
}
Problem p00277 (C++)

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

using namespace std;

int main() {
  int N, R, L;
  cin >> N >> R >> L;
  vector<long long int> data(N + 1, 0);
  vector<int> timet(N + 1, 0);
  int maxteam = 1;
  int maxs = 0;
  int time = 0;
  int d, t, x;
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    timet[maxteam] += t - time;
    data[d] += x;
    time = t;
    maxteam = 1;
    maxs = data[1];
    for (int j = 2; j <= N; j++) {
      if (data[j] > maxs) {
        maxs = data[j];
        maxteam = j;
      }
    }
  }
  timet[maxteam] += L - time;
  int k, ktoku;
  k = 1;
  ktoku = timet[1];
  for (int i = 2; i <= N; i++) {
    if (timet[i] > ktoku) {
      ktoku = timet[i];
      k = i;
    }
  }
  cout << k << endl;
  return 0;
}

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

using namespace std;

int main() {
  int N, R, L;
  cin >> N >> R >> L;
  vector<long long int> data(N + 1, 0);
  vector<int> timet(N + 1, 0);
  int maxteam = 1;
  int maxs = 0;
  int time = 0;
  int d, t, x;
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    timet[maxteam] += t - time;
    data[d] += x;
    time = t;
    if (d == maxteam && x >= 0) {
      maxs = data[d];
    } else if (d != maxteam && data[d] < maxs) {
    } else if (data[d] == maxs && d < maxteam) {
      maxteam = d;
    } else {
      maxteam = 1;
      maxs = data[1];
      for (int j = 2; j <= N; j++) {
        if (data[j] > maxs) {
          maxs = data[j];
          maxteam = j;
        }
      }
    }
  }
  timet[maxteam] += L - time;
  int k, ktoku;
  k = 1;
  ktoku = timet[1];
  for (int i = 2; i <= N; i++) {
    if (timet[i] > ktoku) {
      ktoku = timet[i];
      k = i;
    }
  }
  cout << k << endl;
  return 0;
}
Problem p00277 (C++)

Buggy submission (status: Time Limit Exceeded):
// 2013/11/05 Tazoe

#include <iostream>
using namespace std;

int main() {
  int N, R, L;
  cin >> N >> R >> L;

  long long P[100001] = {0};
  int W = 1;

  int T[100001] = {0};
  int tp = 0;

  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;

    T[W] += t - tp;
    tp = t;

    P[d] += x;

    if (x >= 0) {
      if (P[W] < P[d] || (P[W] == P[d] && W > d)) {
        W = d;
      }
    } else {
      W = 1;
      for (int j = 2; j <= N; j++) {
        if (P[W] < P[j]) {
          W = j;
        }
      }
    }
    //		cout << W << ' ' << P[W] << endl;
  }

  T[W] += L - tp;

  int C = 1;
  for (int i = 2; i <= N; i++) {
    if (T[C] < T[i]) {
      C = i;
    }
  }
  cout << C << endl;

  return 0;
}

Fixed submission:
// 2013/11/05 Tazoe

#include <iostream>
using namespace std;

int main() {
  int N, R, L;
  cin >> N >> R >> L;

  long long P[100001] = {0};
  int W = 1;

  int T[100001] = {0};
  int tp = 0;

  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;

    T[W] += t - tp;
    tp = t;

    P[d] += x;

    if (x >= 0) {
      if (P[W] < P[d] || (P[W] == P[d] && W > d)) {
        W = d;
      }
    } else {
      if (W == d) {
        W = 1;
        for (int j = 2; j <= N; j++) {
          if (P[W] < P[j]) {
            W = j;
          }
        }
      }
    }
    //		cout << W << ' ' << P[W] << endl;
  }

  T[W] += L - tp;

  int C = 1;
  for (int i = 2; i <= N; i++) {
    if (T[C] < T[i]) {
      C = i;
    }
  }
  cout << C << endl;

  return 0;
}
Problem p00280 (C++)

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

using namespace std;

int J, Y;

void t(int j, int y, string str) {
  if (j > J && y > Y)
    return;
  if (j == J && y == Y) {
    cout << str << endl;
    return;
  }
  if (j == 5 && y == 5) {
    return;
  }
  if ((j <= 3 && y >= 5) || (j >= 5 && y <= 3)) {
    return;
  }

  t(j + 1, y, str + "A");
  t(j, y + 1, str + "B");
}

int main() {
  scanf("%d %d", &J, &Y);

  t(0, 0, "");
}

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

using namespace std;

int J, Y;

void t(int j, int y, string str) {
  if (j > J || y > Y)
    return;
  if (j == J && y == Y) {
    cout << str << endl;
    return;
  }
  if (j == 5 && y == 5) {
    return;
  }
  if ((j <= 3 && y >= 5) || (j >= 5 && y <= 3)) {
    return;
  }

  t(j + 1, y, str + "A");
  t(j, y + 1, str + "B");
}

int main() {
  scanf("%d %d", &J, &Y);

  t(0, 0, "");
}
Problem p00280 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
int a, b;
void rec(string s, int x, int y) {
  if (a == x && b == y) {
    cout << s << endl;
    return;
  }
  if ((x < 4 || y < 4) && (x == 5 || y == 5))
    return;
  rec(s + "A", x + 1, y);
  rec(s + "B", x, y + 1);
}
int main() {
  scanf("%d%d", &a, &b);
  rec("", 0, 0);
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int a, b;
void rec(string s, int x, int y) {
  if (a == x && b == y) {
    cout << s << endl;
    return;
  }
  if ((x < 4 || y < 4) && (x == 5 || y == 5))
    return;
  if (x + y > 9)
    return;
  rec(s + "A", x + 1, y);
  rec(s + "B", x, y + 1);
}
int main() {
  scanf("%d%d", &a, &b);
  rec("", 0, 0);
}
Problem p00281 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
using namespace std;
int s[11000];
int t[11000];
int e[11000];
int d[11000];
long long ret[11000];
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  int p, q, r;
  int sz = 0;
  while (scanf("%d%d%d", &p, &q, &r), p) {
    p--;
    q--;
    s[sz] = p;
    t[sz] = q;
    e[sz++] = r;
  }
  int c;
  scanf("%d", &c);
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < b; j++)
      scanf("%d", d + j);
    for (int j = 0; j < a; j++)
      ret[j] = 0LL;
    for (int j = 0; j < sz; j++)
      ret[s[j]] += d[t[j]] * e[j];
    for (int j = 0; j < a; j++) {
      if (j)
        printf(" ");
      printf("%lld", ret[j]);
    }
    printf("\n");
  }
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int s[51000];
int t[51000];
int e[51000];
int d[11000];
long long ret[11000];
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  int p, q, r;
  int sz = 0;
  while (scanf("%d%d%d", &p, &q, &r), p) {
    p--;
    q--;
    s[sz] = p;
    t[sz] = q;
    e[sz++] = r;
  }
  int c;
  scanf("%d", &c);
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < b; j++)
      scanf("%d", d + j);
    for (int j = 0; j < a; j++)
      ret[j] = 0LL;
    for (int j = 0; j < sz; j++)
      ret[s[j]] += d[t[j]] * e[j];
    for (int j = 0; j < a; j++) {
      if (j)
        printf(" ");
      printf("%lld", ret[j]);
    }
    printf("\n");
  }
}
Problem p00281 (C++)

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

using namespace std;

typedef long long ll;

int main() {
  ll N, M, s, t, e;
  cin >> N >> M;
  vector<ll> S, T, E, c(N);
  while (cin >> s >> t >> e, s) {
    S.push_back(s - 1);
    T.push_back(t - 1);
    E.push_back(e);
  }
  ll L;
  cin >> L;
  for (int i = 0; i < L; i++) {
    ll b[L];
    for (int j = 0; j < N; j++) {
      c[j] = 0;
    }
    for (int j = 0; j < M; j++) {
      cin >> b[j];
    }
    for (int j = 0; j < (int)S.size(); j++) {
      c[S[j]] += b[T[j]] * E[j];
    }
    for (int j = 0; j < N; j++) {
      if (j > 0) {
        cout << " ";
      }
      cout << c[j];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ll N, M, s, t, e;
  cin >> N >> M;
  vector<ll> S, T, E, c(N);
  while (cin >> s >> t >> e, s) {
    S.push_back(s - 1);
    T.push_back(t - 1);
    E.push_back(e);
  }
  ll L;
  cin >> L;
  for (int i = 0; i < L; i++) {
    ll b[M];
    for (int j = 0; j < N; j++) {
      c[j] = 0;
    }
    for (int j = 0; j < M; j++) {
      cin >> b[j];
    }
    for (int j = 0; j < (int)S.size(); j++) {
      c[S[j]] += b[T[j]] * E[j];
    }
    for (int j = 0; j < N; j++) {
      if (j > 0) {
        cout << " ";
      }
      cout << c[j];
    }
    cout << endl;
  }
  return 0;
}
Problem p00283 (C++)

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

int D, N;
vector<int> a(110), e(110), r(110); // 耐久力 経験値 必要な経験値
int dp[110][11000]; // 耐久力 i の時に 経験値 j の時の回数

int solve(int d, int exp) {
  int ret = 10e8;
  if (d <= 0)
    return 0;
  if (dp[d][exp] != -1)
    return dp[d][exp];
  for (int i = 0; i < N; i++) {
    int E = min(110, exp + e[i]);
    if (r[i] <= exp)
      ret = min(ret, solve(d - a[i], E) + 1);
  }
  dp[d][exp] = ret;
  return ret;
}

int main() {
  while (cin >> D >> N, D + N) {
    for (int i = 0; i < N; i++)
      cin >> a[i] >> e[i] >> r[i];
    for (int i = 0; i < 110; i++)
      for (int j = 0; j < 11000; j++)
        dp[i][j] = -1;
    int ret = solve(D, 0);
    if (ret == 10e8)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}

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

int D, N;
vector<int> a(110), e(110), r(110); // 耐久力 経験値 必要な経験値
int dp[110][11000]; // 耐久力 i の時に 経験値 j の時の回数

int solve(int d, int exp) {
  int ret = 10e8;
  if (d <= 0)
    return 0;
  if (dp[d][exp] != -1)
    return dp[d][exp];
  for (int i = 0; i < N; i++) {
    int E = min(110, exp + e[i]);
    if (a[i] == 0 && E == exp)
      continue;
    if (r[i] <= exp)
      ret = min(ret, solve(d - a[i], E) + 1);
  }
  dp[d][exp] = ret;
  return ret;
}

int main() {
  while (cin >> D >> N, D + N) {
    for (int i = 0; i < N; i++)
      cin >> a[i] >> e[i] >> r[i];
    for (int i = 0; i < 110; i++)
      for (int j = 0; j < 11000; j++)
        dp[i][j] = -1;
    int ret = solve(D, 0);
    if (ret == 10e8)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}