prithivMLmods/Coder-Stat huggingface dataset:
Problem p00494 (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<char, int> P;

int main() {
  vector<P> v;
  string s;
  cin >> s;
  if (s.size() <= 2) {
    puts("0");
    return 0;
  }
  int cnt = 0;
  char back = s[0];
  for (char c : s) {
    if (c == back)
      cnt++;
    else {
      v.push_back(P(back, cnt));
      cnt = 1;
      back = c;
    }
  }
  v.push_back(P(back, cnt));
  int Max = 0;
  rep(i, v.size() - 2) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I' &&
        v[i + 1].second <= v[i].second && v[i + 1].second <= v[i + 2].second) {
      Max = max(Max, v[i + 1].second);
    }
  }
  cout << Max << endl;
  return 0;
}

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<char, int> P;

int main() {
  vector<P> v;
  string s;
  cin >> s;
  if (s.size() <= 2) {
    puts("0");
    return 0;
  }
  int cnt = 0;
  char back = s[0];
  for (char c : s) {
    if (c == back)
      cnt++;
    else {
      v.push_back(P(back, cnt));
      cnt = 1;
      back = c;
    }
  }
  v.push_back(P(back, cnt));
  int Max = 0;
  if (v.size() <= 2) {
    puts("0");
    return 0;
  }
  rep(i, v.size() - 2) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I' &&
        v[i + 1].second <= v[i].second && v[i + 1].second <= v[i + 2].second) {
      Max = max(Max, v[i + 1].second);
    }
  }
  cout << Max << endl;
  return 0;
}
Problem p00494 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
char c[1000000];
int main() {

  int o, flag, ma = 0;
  scanf("%s", c);

  for (int i = 1; i < strlen(c) - 1; i++) {
    if (c[i] == 'O') {
      o = 1;
      for (int j = i + 1; j < strlen(c) - 1; j++) {
        if (c[j] == 'O')
          o++;
        else
          break;
      }
      flag = 0;
      for (int j = 1; j <= o; j++) {
        if (!(c[i - j] == 'J' && c[i + o - 1 + j] == 'I')) {
          flag++;
          break;
        }
      }
      if (flag == 0) {
        ma = max(ma, o);
      }
    }
  }

  printf("%d\n", ma);
  return (0);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
char c[1000000];
int main() {

  int o, flag, ma = 0;
  scanf("%s", c);

  for (int i = 1; i < strlen(c) - 1; i++) {
    if (c[i] == 'O') {
      o = 1;
      for (int j = i + 1; j < strlen(c) - 1; j++) {
        if (c[j] == 'O')
          o++;
        else
          break;
      }
      flag = 0;
      for (int j = 1; j <= o; j++) {
        if (!(c[i - j] == 'J' && c[i + o - 1 + j] == 'I')) {
          flag++;
          break;
        }
      }
      if (flag == 0) {
        ma = max(ma, o);
      }
      i += o - 1;
    }
  }

  printf("%d\n", ma);
  return (0);
}
Problem p00494 (C++)

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

struct Type {
  char ch;
  int len;
};

void Solve() {
  string s;
  cin >> s;

  vector<Type> cmp;
  char ch = s[0];
  int cnt = 1;
  rep(i, 1, s.size()) {
    if (s[i] != ch) {
      cmp.push_back(Type{ch, cnt});
      ch = s[i];
      cnt = 1;
    } else
      ++cnt;
  }
  cmp.push_back(Type{ch, cnt});

  int ans = 0;
  rep(i, 0, cmp.size() - 2) {
    if (cmp[i].ch == 'J' && cmp[i + 1].ch == 'O' && cmp[i + 2].ch == 'I' &&
        cmp[i].len >= cmp[i + 1].len && cmp[i + 1].len <= cmp[i + 2].len)
      ans = max(ans, cmp[i + 1].len);
  }

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

struct Type {
  char ch;
  int len;
};

void Solve() {
  string s;
  cin >> s;

  vector<Type> cmp;
  char ch = s[0];
  int cnt = 1;
  rep(i, 1, s.size()) {
    if (s[i] != ch) {
      cmp.push_back(Type{ch, cnt});
      ch = s[i];
      cnt = 1;
    } else
      ++cnt;
  }
  cmp.push_back(Type{ch, cnt});

  int ans = 0;
  rep(i, 0, (int)cmp.size() - 2) {
    if (cmp[i].ch == 'J' && cmp[i + 1].ch == 'O' && cmp[i + 2].ch == 'I' &&
        cmp[i].len >= cmp[i + 1].len && cmp[i + 1].len <= cmp[i + 2].len)
      ans = max(ans, cmp[i + 1].len);
  }

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}
Problem p00495 (C++)

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

int main() {
  int a, b;
  cin >> a >> b;
  vector<int> an(a), br(b);
  for (auto &i : an)
    cin >> i;
  for (auto &i : br)
    cin >> i;
  vector<vector<int>> dp(a + 1, vector<int>(b + 1));
  int res = 0;
  for (auto i = 0; i < b; i++) {
    int use = 0;
    for (auto &j : an) {
      if (j == br[use + i])
        use++;
      if (use == b)
        break;
    }
    res = max(res, use);
  }
  cout << res << endl;
}

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

int main() {
  int a, b;
  cin >> a >> b;
  vector<int> an(a), br(b);
  for (auto &i : an)
    cin >> i;
  for (auto &i : br)
    cin >> i;
  int res = 0;
  for (auto i = 0; i < b; i++) {
    int use = 0;
    for (auto &j : an) {
      if (j == br[use + i])
        use++;
      if (use == b)
        break;
    }
    res = max(res, use);
  }
  cout << res << endl;
}
Problem p00495 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
using namespace std;
int field[2][5001] = {0};
int main(void) {
  int current = 1;
  int n_a, n_b;
  scanf("%d%d", &n_a, &n_b);
  int a[5001];
  for (int i = 1; i <= n_a; i++)
    scanf("%d", &a[i]);
  int b[5001];
  for (int i = 1; i <= n_b; i++)
    scanf("%d", &b[i]);
  for (int i = 1; i <= n_a; i++) {
    for (int j = 1; j <= n_b; j++) {
      field[current][j] = field[abs(current - 1)][j];
      if (a[i] == b[j] &&
          field[current][j] <= field[abs(current - 1)][j - 1] + 1)
        field[current][j] = field[abs(current - 1)][j - 1] + 1;
    }
    current = 1 - current;
  }
  int max = 0;
  for (int i = 1; i <= 2; i++) {
    for (int j = 1; j <= n_b; j++) {
      if (max <= field[i][j])
        max = field[i][j];
    }
  }
  printf("%d\n", max);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
using namespace std;
int field[2][5001] = {0};
int main(void) {
  int current = 1;
  int n_a, n_b;
  scanf("%d%d", &n_a, &n_b);
  int a[5001];
  for (int i = 1; i <= n_a; i++)
    scanf("%d", &a[i]);
  int b[5001];
  for (int i = 1; i <= n_b; i++)
    scanf("%d", &b[i]);
  for (int i = 1; i <= n_a; i++) {
    for (int j = 1; j <= n_b; j++) {
      field[current][j] = field[abs(current - 1)][j];
      if (a[i] == b[j] &&
          field[current][j] <= field[abs(current - 1)][j - 1] + 1)
        field[current][j] = field[abs(current - 1)][j - 1] + 1;
    }
    current = 1 - current;
  }
  int max = 0;
  for (int i = 0; i <= 1; i++) {
    for (int j = 1; j <= n_b; j++) {
      if (max <= field[i][j])
        max = field[i][j];
    }
  }
  printf("%d\n", max);
  return 0;
}
Problem p00495 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <cstring>
#define rep(i, n) for (int i = 1; i <= (n); i++)
#define max(a, b) (((a) > (b)) ? (a) : (b))

int dat[5005][2];
int dp[5005][5005];
int A, B;
int main(void) {
  scanf("%d%d", &A, &B);
  rep(i, A) scanf("%d", &(dat[i][0]));
  rep(i, B) scanf("%d", &(dat[i][1]));
  rep(i, A) {
    rep(j, B) {
      dp[i][j] = max(dp[i - 1][j],
                     ((dat[i][0] == dat[j][1]) ? (dp[i - 1][j - 1] + 1) : 0));
      // printf("%d  %d  %d\n",i,j,dp[i][j]);
    }
  }
  int ans = 0;
  rep(i, B) ans = max(ans, dp[A][i]);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#define rep(i, n) for (int i = 1; i <= (n); i++)
#define max(a, b) (((a) > (b)) ? (a) : (b))

int dat[5005][2];
short int dp[5005][5005];
int A, B;
int main(void) {
  scanf("%d%d", &A, &B);
  rep(i, A) scanf("%d", &(dat[i][0]));
  rep(i, B) scanf("%d", &(dat[i][1]));
  rep(i, A) {
    rep(j, B) {
      dp[i][j] = max(dp[i - 1][j],
                     ((dat[i][0] == dat[j][1]) ? (dp[i - 1][j - 1] + 1) : 0));
      // printf("%d  %d  %d\n",i,j,dp[i][j]);
    }
  }
  int ans = 0;
  rep(i, B) ans = max(ans, dp[A][i]);
  printf("%d\n", ans);
  return 0;
}
Problem p00495 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <stdio.h>
int dp[5001][5001];
int a[5000];
int b[5000];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int i, j;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < m; i++)
    scanf("%d", &b[i]);
  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      dp[i][j] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dp[i][j] = dp[i - 1][j];
      if (a[i - 1] == b[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }
  int ans = 0;
  for (i = 0; i <= m; i++)
    ans = max(ans, dp[n][i]);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <stdio.h>
short dp[5001][5001];
int a[5000];
int b[5000];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int i, j;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < m; i++)
    scanf("%d", &b[i]);
  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      dp[i][j] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dp[i][j] = dp[i - 1][j];
      if (a[i - 1] == b[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
    }
  }
  int ans = 0;
  for (i = 0; i <= m; i++)
    ans = max(ans, dp[n][i]);
  printf("%d\n", ans);
  return 0;
}
Problem p00495 (C++)

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

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

using namespace std;

int m, n, a[5000], b[5000];

int dp[5000][5000];
int dfs(int i, int j) {
  if (i == m || j == n)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (a[i] == b[j])
    return dp[i][j] = dfs(i + 1, j + 1) + 1;
  else
    return dp[i][j] = dfs(i + 1, j);
}

int main() {
  scanf("%d%d", &m, &n);
  rep(i, m) scanf("%d", a + i);
  rep(j, n) scanf("%d", b + j);

  memset(dp, -1, sizeof dp);
  int ans = 0;
  rep(j, n) ans = max(ans, dfs(0, j));
  printf("%d\n", ans);

  return 0;
}

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

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

using namespace std;

int m, n, a[5000], b[5000];

short dp[5000][5000];
int dfs(int i, int j) {
  if (i == m || j == n)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (a[i] == b[j])
    return dp[i][j] = dfs(i + 1, j + 1) + 1;
  else
    return dp[i][j] = dfs(i + 1, j);
}

int main() {
  scanf("%d%d", &m, &n);
  rep(i, m) scanf("%d", a + i);
  rep(j, n) scanf("%d", b + j);

  memset(dp, -1, sizeof dp);
  int ans = 0;
  rep(j, n) ans = max(ans, dfs(0, j));
  printf("%d\n", ans);

  return 0;
}
Problem p00496 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <chrono> //1e+9??§?????? auto end= chrono::system_clock::now()-st;  cout<<end.count()%1e+9<<endl;
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll N, S, T;
ll V[3001], C[3001];
ll dp1[3002][3001]; // i?????§???j????????\??????????\???????
ll dp2[3002][3001];
int main() {
  cin >> N >> T >> S;
  T -= S;
  rep1(i, N) { cin >> V[i] >> C[i]; }
  rep1(i, N) {
    for (ll j = 0; j <= S; j++) {
      if (j - C[i] >= 0) {
        dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - C[i]] + V[i]);
      } else
        dp1[i][j] = dp1[i - 1][j];
    }
  }
  rrep1(i, N) {
    for (ll j = 0; j <= T; j++) {
      if (j - C[i] >= 0) {
        dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - C[i]] + V[i]);
      } else
        dp2[i][j] = dp2[i + 1][j];
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= N; i++) {
    ans = max(ans, dp1[i][S] + dp2[i + 1][T]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <chrono> //1e+9??§?????? auto end= chrono::system_clock::now()-st;  cout<<end.count()%1e+9<<endl;
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll N, S, T;
ll V[3001], C[3001];
ll dp1[3002][3001]; // i?????§???j????????\??????????\???????
ll dp2[3002][3001];
int main() {
  cin >> N >> T >> S;
  T -= S;
  rep1(i, N) { cin >> V[i] >> C[i]; }
  rep1(i, N) {
    for (ll j = 0; j <= S; j++) {
      if (j - C[i] >= 0) {
        dp1[i][j] = max(dp1[i - 1][j], dp1[i - 1][j - C[i]] + V[i]);
      } else
        dp1[i][j] = dp1[i - 1][j];
    }
  }
  rrep1(i, N) {
    for (ll j = 0; j <= T; j++) {
      if (j - C[i] >= 0) {
        dp2[i][j] = max(dp2[i + 1][j], dp2[i + 1][j - C[i]] + V[i]);
      } else
        dp2[i][j] = dp2[i + 1][j];
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= N; i++) {
    ans = max(ans, dp1[i][S] + dp2[i + 1][T]);
  }
  cout << ans << endl;
}
Problem p00496 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
using namespace std;

int n, t, s, a[3010], b[3010];
int dp[3010][3010];

signed main() {
  cin >> n >> t >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }

  for (int i = 1; i <= t; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (0 <= i - b[j] && i <= s || s <= i - b[j]) {
        dp[i][j] = max(dp[i][j], dp[i - b[j]][j - 1] + a[j]);
      }
    }
  }

  cout << dp[t][n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #define int long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
using namespace std;

int n, t, s, a[3010], b[3010];
int dp[3010][3010];

signed main() {
  cin >> n >> t >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }

  for (int i = 1; i <= t; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      if (0 <= i - b[j] && i <= s || s <= i - b[j]) {
        dp[i][j] = max(dp[i][j], dp[i - b[j]][j - 1] + a[j]);
      }
    }
  }

  cout << dp[t][n] << endl;
  return 0;
}
Problem p00496 (C++)

Buggy submission (status: Memory Limit Exceeded):

#include <algorithm>
#include <iostream>
using namespace std;

int mem[10000][10000], A[10000], B[10000], N, T, S;
int main() {
  int ans = 0;
  cin >> N >> T >> S;
  for (int i = 1; i <= N; i++)
    cin >> A[i] >> B[i];

  for (int n = 1; n <= N; n++) {
    for (int t = 1; t <= T; t++) {
      mem[n][t] = max(mem[n - 1][t], mem[n][t - 1]);
      if (!(t - B[n] < S && t > S) && t >= B[n])
        mem[n][t] = max(mem[n - 1][t - B[n]] + A[n], mem[n][t]);
    }
  }
  cout << mem[N][T] << endl;
}

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int mem[5000][5000], A[5000], B[5000], N, T, S;
int main() {
  int ans = 0;
  cin >> N >> T >> S;
  for (int i = 1; i <= N; i++)
    cin >> A[i] >> B[i];

  for (int n = 1; n <= N; n++) {
    for (int t = 1; t <= T; t++) {
      mem[n][t] = max(mem[n - 1][t], mem[n][t - 1]);
      if (!(t - B[n] < S && t > S) && t >= B[n])
        mem[n][t] = max(mem[n - 1][t - B[n]] + A[n], mem[n][t]);
    }
  }
  cout << mem[N][T] << endl;
}
Problem p00496 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

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

int N, T, S;
int A[4096], B[4096];
int dp[4096][4096];

int dfs(int n, int t) {
  if (n >= N)
    return 0;
  if (t > T)
    return 0;

  int &res = dp[n][t];
  if (res >= 0)
    return res;
  res = 0;
  if (t + B[n] <= T && (S <= t || t + B[n] <= S))
    res = max(res, dfs(n + 1, t + B[n]) + A[n]);
  else if (t + B[n] <= T && (t < S && S < t + B[n]) && S + B[n] <= T)
    res = max(res, dfs(n + 1, S + B[n]) + A[n]);
  res = max(res, dfs(n + 1, t));

  return res;
}

int main() {
  scanf("%d%d%d", &N, &T, &S);
  rep(i, N) scanf("%d%d", &A[i], &B[i]);
  memset(dp, -1, sizeof(dp));
  printf("%d\n", dfs(0, 0));
}

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

using namespace std;

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

int N, T, S;
int A[3001], B[3001];
int dp[3001][3001];

int dfs(int n, int t) {
  if (n >= N)
    return 0;
  if (t > T)
    return 0;

  int &res = dp[n][t];
  if (res >= 0)
    return res;
  res = 0;
  if (t + B[n] <= T && (S <= t || t + B[n] <= S))
    res = max(res, dfs(n + 1, t + B[n]) + A[n]);
  else if (t + B[n] <= T && (t < S && S < t + B[n]) && S + B[n] <= T)
    res = max(res, dfs(n + 1, S + B[n]) + A[n]);
  res = max(res, dfs(n + 1, t));

  return res;
}

int main() {
  scanf("%d%d%d", &N, &T, &S);
  rep(i, N) scanf("%d%d", &A[i], &B[i]);
  memset(dp, -1, sizeof(dp));
  printf("%d\n", dfs(0, 0));
}
Problem p00496 (C++)

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, t, s;
  cin >> n >> t >> s;

  vector<int> a(n), b(n);
  rep(i, 0, n) cin >> a[i] >> b[i];

  static ll dp[3000][3001];
  ll ans = 0;
  rep(i, 0, n) {
    rep(j, 0, t + 1) {
      if (i != 0)
        dp[i][j] = dp[i - 1][j];
      if (j - b[i] >= 0 && !(j - b[i] < s && s < j)) {
        if (i != 0)
          dp[i][j] = max(dp[i][j], dp[i - 1][j - b[i]] + a[i]);
        else
          dp[i][j] = a[i];
      }
      ans = max(ans, dp[i][j]);
    }
  }

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, t, s;
  cin >> n >> t >> s;

  vector<int> a(n), b(n);
  rep(i, 0, n) cin >> a[i] >> b[i];

  static int dp[3000][3001];
  int ans = 0;
  rep(i, 0, n) {
    rep(j, 0, t + 1) {
      if (i != 0)
        dp[i][j] = dp[i - 1][j];
      if (j - b[i] >= 0 && !(j - b[i] < s && s < j)) {
        if (i != 0)
          dp[i][j] = max(dp[i][j], dp[i - 1][j - b[i]] + a[i]);
        else
          dp[i][j] = a[i];
      }
      ans = max(ans, dp[i][j]);
    }
  }

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}
Problem p00497 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
short flag[5000][5000];
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    int A, B, C;
    scanf("%d %d %d", &A, &B, &C);
    flag[A][B] = max<short>(flag[A][B], C + 1);
  }
  int ret = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i; j++) {
      flag[i][j] =
          max<short>(flag[i][j], max(flag[i - 1][j - 1], flag[i - 1][j]) - 1);
      if (flag[i][j] > 0)
        ret++;
    }
  }
  cout << ret << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
short flag[5005][5005];
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    int A, B, C;
    scanf("%d %d %d", &A, &B, &C);
    flag[A][B] = max<short>(flag[A][B], C + 1);
  }
  int ret = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i; j++) {
      flag[i][j] =
          max<short>(flag[i][j], max(flag[i - 1][j - 1], flag[i - 1][j]) - 1);
      if (flag[i][j] > 0)
        ret++;
    }
  }
  cout << ret << endl;
}
Problem p00497 (C++)

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

#define N 2020

vector<int> x[N];

int n, m, a, b, c, cnt;
int V[N];

int main() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i + 5; j++) {
      x[i].push_back(0);
      V[i] = j;
    }
  }
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    c++;
    x[a][b]++;
    x[a + c][b]--;
    x[a + c + 1][b + 1]++;
    x[a][b + 1]--;
    x[a + c][b + c + 1]++;
    x[a + c + 1][b + c + 1]--;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < N; j++) {
      if (j < V[i]) {
        x[i][j] += x[i][j - 1];
      }
    }
  }
  for (int j = 0; j < N; j++) {
    for (int i = 1; i < N; i++) {
      if (j < V[i]) {
        x[i][j] += x[i - 1][j];
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n - i; j++) {
      if (j + 1 < V[i + j + 1]) {
        x[i + j + 1][j + 1] += x[i + j][j];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j < V[i]) {
        if (x[i][j] >= 1) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}

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

#define N 5100

vector<int> x[N];

int n, m, a, b, c, cnt;
int V[N];

int main() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i + 5; j++) {
      x[i].push_back(0);
      V[i] = j;
    }
  }
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    c++;
    x[a][b]++;
    x[a + c][b]--;
    x[a + c + 1][b + 1]++;
    x[a][b + 1]--;
    x[a + c][b + c + 1]++;
    x[a + c + 1][b + c + 1]--;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < N; j++) {
      if (j < V[i]) {
        x[i][j] += x[i][j - 1];
      }
    }
  }
  for (int j = 0; j < N; j++) {
    for (int i = 1; i < N; i++) {
      if (j < V[i]) {
        x[i][j] += x[i - 1][j];
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n - i; j++) {
      if (j + 1 < V[i + j + 1]) {
        x[i + j + 1][j + 1] += x[i + j][j];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j < V[i]) {
        if (x[i][j] >= 1) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p00497 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"

int main() {
  int N;
  cin >> N;
  int M;
  cin >> M;
  vector<vector<pair<int, int>>> pluss(N, vector<pair<int, int>>(N + 1));
  vector<vector<int>> aas(N, vector<int>(N + 1));
  for (int i = 0; i < M; ++i) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    pluss[a + x][b].first++;
    pluss[a + x][b + x + 1].second++;
    aas[a - 1][b]++;
  }
  for (int h = N - 1; h >= 0; --h) {
    for (int w = 0; w <= h + 1; ++w) {
      if (h != N - 1) {

        pluss[h][w].first += pluss[h + 1][w].first;
        pluss[h][w].second += pluss[h + 1][w + 1].second;
      }
      pluss[h][w].first -= aas[h][w];
      pluss[h][w].second -= aas[h][w];
    }
  }
  int ans = 0;
  for (int h = N - 1; h >= 0; --h) {
    int sum = 0;
    for (int w = 0; w <= h; ++w) {
      sum += pluss[h][w].first - pluss[h][w].second;
      if (sum > 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"

int main() {
  int N;
  cin >> N;
  int M;
  cin >> M;
  vector<vector<pair<int, int>>> pluss(N, vector<pair<int, int>>(N + 1));
  vector<vector<int>> aas(N, vector<int>(N + 1));
  for (int i = 0; i < M; ++i) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    pluss[a + x][b].first++;
    pluss[a + x][b + x + 1].second++;
    if (a) {

      aas[a - 1][b]++;
    }
  }
  for (int h = N - 1; h >= 0; --h) {
    for (int w = 0; w <= h + 1; ++w) {
      if (h != N - 1) {

        pluss[h][w].first += pluss[h + 1][w].first;
        pluss[h][w].second += pluss[h + 1][w + 1].second;
      }
      pluss[h][w].first -= aas[h][w];
      pluss[h][w].second -= aas[h][w];
    }
  }
  int ans = 0;
  for (int h = N - 1; h >= 0; --h) {
    int sum = 0;
    for (int w = 0; w <= h; ++w) {
      sum += pluss[h][w].first - pluss[h][w].second;
      if (sum > 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00497 (C++)

Buggy submission (status: Runtime Error):

#include <stdio.h>
#include <string.h>

int plate[5002][5002];

int n, m;

int main() {
  memset(plate, 0, sizeof(plate));
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    --a;
    --b;
    ++plate[a][b];
    --plate[a][b + 1];
    --plate[a + x + 1][b];
    ++plate[a + x + 2][b + 1];
    ++plate[a + x + 1][b + x + 2];
    --plate[a + x + 2][b + x + 2];
  }
  for (int i = 1; i <= n + 2; ++i) {
    for (int j = 1; j <= n + 2; ++j) {
      plate[i][j] += plate[i - 1][j - 1];
    }
  }
  for (int i = 0; i <= n + 2; ++i) {
    for (int j = 1; j <= n + 2; ++j) {
      plate[i][j] += plate[i][j - 1];
    }
  }
  for (int i = 1; i <= n + 2; ++i) {
    for (int j = 0; j <= n + 2; ++j) {
      plate[i][j] += plate[i - 1][j];
    }
  }

  int ans = 0;
  for (int i = 0; i <= n + 2; ++i) {
    for (int j = 0; j <= n + 2; ++j) {
      if (plate[i][j] > 0)
        ++ans;
    }
  }

  printf("%d\n", ans);
  return 0;
}

Fixed submission:

#include <stdio.h>
#include <string.h>

int plate[5678][5678];

int n, m;

int main() {
  memset(plate, 0, sizeof(plate));
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    --a;
    --b;
    ++plate[a][b];
    --plate[a][b + 1];
    --plate[a + x + 1][b];
    ++plate[a + x + 2][b + 1];
    ++plate[a + x + 1][b + x + 2];
    --plate[a + x + 2][b + x + 2];
  }
  for (int i = 1; i <= n + 2; ++i) {
    for (int j = 1; j <= n + 2; ++j) {
      plate[i][j] += plate[i - 1][j - 1];
    }
  }
  for (int i = 0; i <= n + 2; ++i) {
    for (int j = 1; j <= n + 2; ++j) {
      plate[i][j] += plate[i][j - 1];
    }
  }
  for (int i = 1; i <= n + 2; ++i) {
    for (int j = 0; j <= n + 2; ++j) {
      plate[i][j] += plate[i - 1][j];
    }
  }

  int ans = 0;
  for (int i = 0; i <= n + 2; ++i) {
    for (int j = 0; j <= n + 2; ++j) {
      if (plate[i][j] > 0)
        ++ans;
    }
  }

  printf("%d\n", ans);
  return 0;
}
Problem p00497 (C++)

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

using namespace std;

const double EPS = (1e-10);
typedef long long ll;
typedef pair<int, int> pii;
bool EQ(double a, double b) { return abs((a) - (b)) < EPS; }
void fast_stream() { std::ios_base::sync_with_stdio(0); }

short dp[5001][5001];
void dfs(int x, int y, int depth) {
  if (dp[y][x] >= depth)
    return;
  dp[y][x] = depth;
  if (depth == 0)
    return;
  int ny = y + 1;
  int nx = x + 1;
  dfs(nx, ny, depth - 1);
  ny = y;
  dfs(nx, ny, depth - 1);
}

void solve() {
  memset(dp, -1, sizeof(dp));
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, a;
    cin >> x >> y >> a;
    dp[x][y] = a;
    // dfs(x,y,a);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], (short)(dp[i][j] - 1));
      dp[i + 1][j] = max(dp[i + 1][j], (short)(dp[i][j] - 1));
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      if (dp[i][j] >= 0)
        cnt++;
    }
  }
  cout << cnt << endl;
}

int main() {

  solve();

  return 0;
}

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

using namespace std;

const double EPS = (1e-10);
typedef long long ll;
typedef pair<int, int> pii;
bool EQ(double a, double b) { return abs((a) - (b)) < EPS; }
void fast_stream() { std::ios_base::sync_with_stdio(0); }

short dp[5002][5002];
void dfs(int x, int y, int depth) {
  if (dp[y][x] >= depth)
    return;
  dp[y][x] = depth;
  if (depth == 0)
    return;
  int ny = y + 1;
  int nx = x + 1;
  dfs(nx, ny, depth - 1);
  ny = y;
  dfs(nx, ny, depth - 1);
}

void solve() {
  memset(dp, -1, sizeof(dp));
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, a;
    cin >> x >> y >> a;
    dp[x][y] = a;
    // dfs(x,y,a);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], (short)(dp[i][j] - 1));
      dp[i + 1][j] = max(dp[i + 1][j], (short)(dp[i][j] - 1));
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      if (dp[i][j] >= 0)
        cnt++;
    }
  }
  cout << cnt << endl;
}

int main() {

  solve();

  return 0;
}
Problem p00497 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> plus_[5001], minus_[5001];

int main() {
  int N, M, A, B, X;
  cin >> N >> M;
  for (int i = 0; i <= N; i++) {
    plus_[i].assign(i + 2, 0);
    minus_[i].assign(i + 2, 0);
  }
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> X;
    A--;
    B--;
    plus_[A][B]++;
    plus_[A + X + 1][B]--;
    minus_[A][B]--;
    minus_[A + X + 1][B + X + 1]++;
  }
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j < N - i; j++) {
      sum += plus_[j + i][i];
      plus_[j + i][i] = sum;
    }
  }
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j < N - i; j++) {
      sum += minus_[i + j][j];
      minus_[i + j][j] = sum;
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j <= i; j++) {
      sum += plus_[i][j];
      if (sum > 0)
        ans++;
      sum += minus_[i][j];
    }
  }
  printf("%d\n", ans);
  return 0;
}

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

vector<short> plus_[5001], minus_[5001];

int main() {
  int N, M, A, B, X;
  cin >> N >> M;
  for (int i = 0; i <= N; i++) {
    plus_[i].assign(i + 2, 0);
    minus_[i].assign(i + 2, 0);
  }
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> X;
    A--;
    B--;
    plus_[A][B]++;
    plus_[A + X + 1][B]--;
    minus_[A][B]--;
    minus_[A + X + 1][B + X + 1]++;
  }
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j < N - i; j++) {
      sum += plus_[j + i][i];
      plus_[j + i][i] = sum;
    }
  }
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j < N - i; j++) {
      sum += minus_[i + j][j];
      minus_[i + j][j] = sum;
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j <= i; j++) {
      sum += plus_[i][j];
      if (sum > 0)
        ans++;
      sum += minus_[i][j];
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p00497 (C++)

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

short dp[5000][5000];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y, c;
    cin >> y >> x >> c;
    dp[y - 1][x - 1] = c + 1;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i + 1][j] = max<int>(dp[i + 1][j], dp[i][j] - 1);
      dp[i + 1][j + 1] = max<int>(dp[i + 1][j + 1], dp[i][j] - 1);
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ans += !!dp[i][j];
    }
  }
  cout << ans << endl;
}

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

short dp[5010][5010];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y, c;
    cin >> y >> x >> c;
    dp[y - 1][x - 1] = c + 1;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i + 1][j] = max<int>(dp[i + 1][j], dp[i][j] - 1);
      dp[i + 1][j + 1] = max<int>(dp[i + 1][j + 1], dp[i][j] - 1);
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ans += !!dp[i][j];
    }
  }
  cout << ans << endl;
}
Problem p00497 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define S 5002
using namespace std;
int l[S][S] = {0};

int main() {
  int N, M, a, b, x, i, j;
  scanf("%d %d", &N, &M);
  for (i = 0; i < M; i++) {
    scanf("%d %d %d", &a, &b, &x);
    l[a][b]++;
    l[a][b + 1]--;
    l[a + x + 1][b]--;
    l[a + x + 2][b + 1]++;
    l[a + x + 2][b + x + 2]--;
    l[a + x + 1][b + x + 2]++;
  }
  for (i = 1; i < N + 4; i++) {
    for (j = 1; j < N + 4; j++) {
      l[i][j] += l[i][j - 1];
    }
  }
  for (j = 1; j < N + 4; j++) {
    for (i = 1; i < N + 4; i++) {
      l[i][j] += l[i - 1][j];
    }
  }
  for (i = 1; i < N + 4; i++) {
    for (j = 1; j < N + 4; j++) {
      if (i + j - 1 < S) {
        l[i + j - 1][j] += l[i + j - 2][j - 1];
      }
    }
  }
  int cnt = 0;
  for (i = 0; i < N + 4; i++) {
    for (j = 0; j < N + 4; j++) {
      if (l[i][j] > 0) {
        cnt++;
      }
    }
  }
  printf("%d\n", cnt);
  return 0;
}


Fixed submission:
#include <cstdio>
#define S 5010
using namespace std;
int l[S][S] = {0};

int main() {
  int N, M, a, b, x, i, j;
  scanf("%d %d", &N, &M);
  for (i = 0; i < M; i++) {
    scanf("%d %d %d", &a, &b, &x);
    l[a][b]++;
    l[a][b + 1]--;
    l[a + x + 1][b]--;
    l[a + x + 2][b + 1]++;
    l[a + x + 2][b + x + 2]--;
    l[a + x + 1][b + x + 2]++;
  }
  for (i = 1; i < N + 4; i++) {
    for (j = 1; j < N + 4; j++) {
      l[i][j] += l[i][j - 1];
    }
  }
  for (j = 1; j < N + 4; j++) {
    for (i = 1; i < N + 4; i++) {
      l[i][j] += l[i - 1][j];
    }
  }
  for (i = 1; i < N + 4; i++) {
    for (j = 1; j < N + 4; j++) {
      if (i + j - 1 < S) {
        l[i + j - 1][j] += l[i + j - 2][j - 1];
      }
    }
  }
  int cnt = 0;
  for (i = 0; i < N + 4; i++) {
    for (j = 0; j < N + 4; j++) {
      if (l[i][j] > 0) {
        cnt++;
      }
    }
  }
  printf("%d\n", cnt);
  return 0;
}
Problem p00497 (C++)

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  const int size = n + 3;
  vector<vector<int>> triangle(size, vector<int>(size, 0));

  for (int i = 0; i < m; ++i) {
    int a, b, x;
    cin >> a >> b >> x;

    ++triangle[a][b];
    --triangle[a][b + 1];
    --triangle[a + x + 1][b];
    ++triangle[a + x + 2][b + 1];
    ++triangle[a + x + 1][b + x + 2];
    --triangle[a + x + 2][b + x + 2];
  }

  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < i; ++j) {
      triangle[i][j + 1] += triangle[i][j];
    }
  }

  for (int i = 0; i < size; ++i) {
    for (int j = i + 1; j < size; ++j) {
      triangle[j][i] += triangle[j - 1][i];
    }
  }

  for (int i = 0; i < size; ++i) {
    for (int j = 1; i + j < size; ++j) {
      triangle[i + j][j] += triangle[i + j - 1][j - 1];
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= i; ++j) {
      if (triangle[i][j] > 0)
        ++ans;
    }
  }

  cout << ans << endl;

  return EXIT_SUCCESS;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  const int size = n + 3;
  vector<vector<int>> triangle;
  for (int i = 0; i < size; ++i)
    triangle.emplace_back(i + 2, 0);

  for (int i = 0; i < m; ++i) {
    int a, b, x;
    cin >> a >> b >> x;

    ++triangle[a][b];
    --triangle[a][b + 1];
    --triangle[a + x + 1][b];
    ++triangle[a + x + 2][b + 1];
    ++triangle[a + x + 1][b + x + 2];
    --triangle[a + x + 2][b + x + 2];
  }

  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < i; ++j) {
      triangle[i][j + 1] += triangle[i][j];
    }
  }

  for (int i = 0; i < size; ++i) {
    for (int j = i + 1; j < size; ++j) {
      triangle[j][i] += triangle[j - 1][i];
    }
  }

  for (int i = 0; i < size; ++i) {
    for (int j = 1; i + j < size; ++j) {
      triangle[i + j][j] += triangle[i + j - 1][j - 1];
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= i; ++j) {
      if (triangle[i][j] > 0)
        ++ans;
    }
  }

  cout << ans << endl;

  return EXIT_SUCCESS;
}
Problem p00497 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <stdio.h>
int t[5000][5000];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, k;
  int i, j;
  scanf("%d %d", &n, &k);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      t[i][j] = -1;
  for (i = 0; i < k; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    t[a - 1][b - 1] = max(t[a - 1][b - 1], c);
  }
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n; j++) {
      if (t[i][j] > 0) {
        t[i + 1][j] = max(t[i + 1][j], t[i][j] - 1);
        if (j < n - 1)
          t[i + 1][j + 1] = max(t[i + 1][j + 1], t[i][j] - 1);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (t[i][j] >= 0)
        ans++;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <stdio.h>
short t[5000][5000];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int n, k;
  int i, j;
  scanf("%d %d", &n, &k);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      t[i][j] = -1;
  for (i = 0; i < k; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    t[a - 1][b - 1] = max(t[a - 1][b - 1], c);
  }
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n; j++) {
      if (t[i][j] > 0) {
        t[i + 1][j] = max(t[i + 1][j], t[i][j] - 1);
        if (j < n - 1)
          t[i + 1][j + 1] = max(t[i + 1][j + 1], t[i][j] - 1);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (t[i][j] >= 0)
        ans++;
  printf("%d\n", ans);
  return 0;
}
Problem p00497 (C++)

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

int main() {
  int a, b, x, n, m, field[5001][5001] = {};
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> x;
    field[a - 1][b - 1] = x + 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      field[i + 1][j] = max(field[i + 1][j], field[i][j] - 1);
      field[i + 1][j + 1] = max(field[i + 1][j + 1], field[i][j] - 1);
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (field[i][j])
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}

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

int main() {
  int a, b, x, n, m;
  static int field[5001][5001] = {};
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> x;
    field[a - 1][b - 1] = x + 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      field[i + 1][j] = max(field[i + 1][j], field[i][j] - 1);
      field[i + 1][j + 1] = max(field[i + 1][j + 1], field[i][j] - 1);
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (field[i][j])
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p00497 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

#define calc(x, y) ((((x) * ((x) + 1)) / 2) + (y))
#define safe_calc(x, y) ((((x) + 2) * ((x) + 3) / 2) + (y) + 1)

int n;
int sum;
int nails[12517506];

void process() {
  int i;

  for (i = 1; i < n + 3; i++) {
    int j;
    for (j = 1; j <= i; j++) {
      nails[calc(i, j)] += nails[calc(i, j) - 1];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 1; j < (n + 3 - i); j++) {
      nails[calc(i + j, i)] += nails[calc(i + j - 1, i)];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 1; j < (n + 3 - i); j++) {
      nails[calc(i + j, j)] += nails[calc(i + j - 1, j - 1)];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 0; j <= i; j++) {
      if (nails[calc(i, j)] > 0)
        sum++;
    }
  }
}

int main() {
  int i;
  int m;

  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    a--;
    b--;
    x++;
    nails[safe_calc(a, b)]++;
    nails[safe_calc(a, b) + 1]--;
    nails[safe_calc(a + x, b)]--;
    nails[safe_calc(a + x, b + x) + 1]++;
    nails[safe_calc(a + x + 1, b + 1)]++;
    nails[safe_calc(a + x + 1, b + x + 1)]--;
  }

  process();

  printf("%d\n", sum);
}

Fixed submission:
#include <stdio.h>

#define calc(x, y) ((((x) * ((x) + 1)) / 2) + (y))
#define safe_calc(x, y) ((((x) + 2) * ((x) + 3) / 2) + (y) + 1)

int n;
int sum;
int nails[13000000];

void process() {
  int i;

  for (i = 1; i < n + 3; i++) {
    int j;
    for (j = 1; j <= i; j++) {
      nails[calc(i, j)] += nails[calc(i, j) - 1];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 1; j < (n + 3 - i); j++) {
      nails[calc(i + j, i)] += nails[calc(i + j - 1, i)];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 1; j < (n + 3 - i); j++) {
      nails[calc(i + j, j)] += nails[calc(i + j - 1, j - 1)];
    }
  }

  for (i = 0; i < n + 3; i++) {
    int j;
    for (j = 0; j <= i; j++) {
      if (nails[calc(i, j)] > 0)
        sum++;
    }
  }
}

int main() {
  int i;
  int m;

  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    int a, b, x;
    scanf("%d %d %d", &a, &b, &x);
    a--;
    b--;
    x++;
    nails[safe_calc(a, b)]++;
    nails[safe_calc(a, b) + 1]--;
    nails[safe_calc(a + x, b)]--;
    nails[safe_calc(a + x, b + x) + 1]++;
    nails[safe_calc(a + x + 1, b + 1)]++;
    nails[safe_calc(a + x + 1, b + x + 1)]--;
  }

  process();

  printf("%d\n", sum);
}
Problem p00499 (C++)

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

using namespace std;

int main() {

  int A, B, C, D, L;
  int date_langue, date_math, date_play;
  int tmp;

  while (1) {
    cin >> L >> A >> B >> C >> D;

    date_langue = A / C;

    tmp = A % C;
    if (tmp > 0)
      date_langue++;

    date_math = B / D;
    tmp = B % D;
    if (tmp > 0)
      date_math++;

    if (date_langue > date_math) {
      date_play = L - date_langue;
    } else
      date_play = L - date_math;

    cout << date_play << endl;
  }
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {

  int A, B, C, D, L;
  int date_langue, date_math, date_play;
  int tmp;

  while (1) {
    cin >> L >> A >> B >> C >> D;

    date_langue = A / C;

    tmp = A % C;
    if (tmp > 0)
      date_langue++;

    date_math = B / D;
    tmp = B % D;
    if (tmp > 0)
      date_math++;

    if (date_langue > date_math) {
      date_play = L - date_langue;
    } else
      date_play = L - date_math;

    cout << date_play << endl;
    break;
  }
}
Problem p00499 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main(void) {
  int a[101], temp = 0, ruio = 0;
  for (int i = 1; i <= 5; i++) {
    scanf("%d", &a[i]);
  }
  if (a[4] < a[5]) {
    a[4] = a[5];
  }
  temp = a[2] + a[3];
  while (temp > 0) {
    temp - a[4];
    ruio++;
  }
  printf("%d\n", a[1] - ruio);
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int a[101], temp = 0, ruio = 0;
  for (int i = 1; i <= 5; i++) {
    scanf("%d", &a[i]);
  }
  while (a[2] > 0 || a[3] > 0) {
    a[2] -= a[4];
    a[3] -= a[5];
    ruio++;
  }
  printf("%d\n", a[1] - ruio);
  return 0;
}