alexjercan/bugnet huggingface dataset:
Problem p01529 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

typedef unsigned long long ull;

int main(void) {

  int n;
  cin >> n;

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

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

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

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

  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

typedef unsigned long long ull;

int main(void) {

  int n;
  cin >> n;

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

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

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

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

  return 0;
}
Problem p01529 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>

#define MAX_N (4000)

using namespace std;

typedef long long lint;

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

  scanf("%d", &N);

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

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

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

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

  return (0);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

#define MAX_N (100)

using namespace std;

typedef long long lint;

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

  scanf("%d", &N);

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

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

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

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

  return (0);
}
Problem p01529 (C++) — buggy submission, verdict: Runtime Error

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

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

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

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

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

  for (ll l = 2; l < N; l++) {
    for (ll i = 0; i + l < N; i++) {
      for (ll k = mo[i][i + l - 1]; k <= mo[i + 1][i + l]; k++) {
        if (dp[i][i + l] == -1 || dp[i][i + l] > dp[i][k] + dp[k + 1][i + l]) {
          dp[i][i + l] = dp[i][k] + dp[k + 1][i + l];
          mo[i][i + l] = k;
        }
      }
      dp[i][i + l] += sum[i + l + 1] - sum[i];
    }
  }
  cout << dp[0][N - 1] << endl;
  return 0;
}
Problem p01529 (C++) — buggy submission, verdict: 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() {
  const int MAX = 4001;
  int n;
  static int k[MAX][MAX];
  static ll s[MAX], dp[MAX][MAX];
  cin >> n;
  rep(i, 0, n) {
    ll w;
    cin >> w;
    s[i + 1] = s[i] + w;
  }

  fill_n((ll *)dp, MAX * MAX, INF_);
  rep(i, 0, n + 1) {
    dp[i][i] = 0;
    k[i][i] = i;
    if (i + 1 <= n) {
      dp[i][i + 1] = 0;
      k[i][i + 1] = i + 1;
    }
  }

  rep(l, 1, n + 1) {
    for (int i = 0, j = i + l; j <= n; ++i, ++j) {
      rep(r, k[i][j - 1], k[i + 1][j] + 1) {
        ll c = dp[i][r] + dp[r][j] + s[j];
        if (i != 0)
          c -= s[i];
        if (dp[i][j] > c) {
          dp[i][j] = c;
          k[i][j] = r;
        }
      }
    }
  }

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

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

Error: MLE

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;
  static int k[4001][4001];
  static ll w[4001], s[4001][4001], dp[4001][4001];
  cin >> n;
  rep(i, 0, n) cin >> w[i];

  rep(i, 0, n + 1) {
    rep(j, i + 1, n + 1) { s[i][j] = s[i][j - 1] + w[j - 1]; }
  }

  fill_n((ll *)dp, 4001 * 4001, INF_);
  rep(i, 0, n + 1) {
    dp[i][i] = 0;
    k[i][i] = i;
    if (i + 1 <= n) {
      dp[i][i + 1] = 0;
      k[i][i + 1] = i + 1;
    }
  }

  rep(l, 1, n + 1) {
    for (int i = 0, j = i + l; j <= n; ++i, ++j) {
      rep(r, k[i][j - 1], k[i + 1][j] + 1) {
        ll c = dp[i][r] + dp[r][j] + s[i][j];
        if (dp[i][j] > c) {
          dp[i][j] = c;
          k[i][j] = r;
        }
      }
    }
  }

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

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}
Problem p01529 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

typedef unsigned long long ull;

ull dp[4001][4001], K[4001][4001];

int main(void) {

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

  fill(dp[0], dp[4001], INF);
  fill(K[0], K[4001], 0);

  for (int i = 0; i < n; i++)
    dp[i][i] = 0, K[i][i] = i;

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 100000000000000LL

using namespace std;

typedef unsigned long long ull;

ull dp[4001][4001];

int main(void) {

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

  fill(dp[0], dp[4001], INF);

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

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

  return 0;
}
Problem p01529 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define ll long long int

using namespace std;

ll w[4001];
ll sum[4001];
ll dp[4001][4001];
int at[4001][4001];

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    if (i == 0)
      sum[i] = w[i];
    else
      sum[i] = sum[i - 1] + w[i];
    at[i][i] = i;
  }
  for (int k = 1; k < n; k++) {
    for (int i = 0; i < n - k; i++) {
      // dp[i][i + k]
      ll mini = 1e18;
      for (int j = at[i][i + k - 1]; j <= at[i + 1][i + k]; j++) {
        if (j < i || j > i + k - 1)
          continue;
        ll tmp = dp[i][j] + dp[j + 1][i + k];
        if (tmp <= mini) {
          mini = tmp;
          at[i][i + k] = j;
        }
      }
      if (i != 0)
        dp[i][i + k] = mini + sum[i + k] - sum[i - 1];
      else
        dp[i][i + k] = mini + sum[k];
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

#define ll long long int

using namespace std;

ll w[4001];
ll sum[4001];
ll dp[4001][4001];
int at[4001][4001];

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    if (i == 0)
      sum[i] = w[i];
    else
      sum[i] = sum[i - 1] + w[i];
  }
  for (int k = 1; k < n; k++) {
    for (int i = 0; i < n - k; i++) {
      // dp[i][i + k]
      ll mini = 1e18;
      for (int j = at[i][i + k - 1]; j < i + k; j++) {
        if (j < i || j > i + k - 1)
          continue;
        ll tmp = dp[i][j] + dp[j + 1][i + k];
        if (tmp < mini) {
          mini = tmp;
          at[i][i + k] = j;
        }
      }
      if (i != 0)
        dp[i][i + k] = mini + sum[i + k] - sum[i - 1];
      else
        dp[i][i + k] = mini + sum[k];
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}
Problem p01529 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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


Error: TLE

Fixed submission:

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

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

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

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

#include <iostream>
using namespace std;
int a[1000], b[1000];
int A, B, C, D, S;
int main() {
  cin >> A >> B >> C >> D;
  for (int i = 0; i < A; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < 1000000; i++) {
    for (int j = 0; j < A; j++) {
      if (a[j] == 0 && b[j] == 0) {
        cout << j + 1 << endl;
        goto E;
      }
      if (a[j] >= 1) {
        S += 10;
        B++;
        a[j]--;
      } else {
        S += 100;
        C++;
        b[j]--;
      }
      if (B > D) {
        cout << j + 1 << endl;
        goto E;
      }
      if (S >= 90) {
        if (B >= 9) {
          B -= S / 10 - 9;
          a[j] += S / 10 - 9;
          S = 0;
        } else {
          cout << j + 1 << endl;
          goto E;
        }
      }
    }
  }
E:;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int a[1000], b[1000];
int A, B, C, D, S;
int main() {
  cin >> A >> B >> C >> D;
  for (int i = 0; i < A; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < 100000000; i++) {
    for (int j = 0; j < A; j++) {
      if (a[j] == 0 && b[j] == 0) {
        cout << j + 1 << endl;
        goto E;
      }
      if (a[j] >= 1) {
        S += 10;
        B++;
        a[j]--;
      } else {
        S += 100;
        C++;
        b[j]--;
      }
      if (B > D) {
        cout << j + 1 << endl;
        goto E;
      }
      if (S >= 90) {
        if (B >= 9) {
          B -= 9;
          a[j] += 9;
          S = 0;
        } else {
          cout << j + 1 << endl;
          goto E;
        }
      }
    }
  }
E:;
  return 0;
}
Problem p01532 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int N, T, H, L, t[100], h[100], c, n = 0;

int main() {
  scanf("%d%d%d%d", &N, &T, &H, &L);
  for (int i = 0; i < N; i++)
    scanf("%d%d", &t[i], &h[i]);
  for (c = 0;; c = (c + 1) % N) {
    if (!t[c] & !h[c])
      break;
    if (!t[c]) {
      h[c]--;
      n += 100;
      if (T < (n - 90) / 10)
        break;
      t[c] += (n - 90) / 10;
      H++;
      T -= (n - 90) / 10;
      n = 0;
    } else {
      t[c]--;
      T++;
      n += 10;
      if (L < T)
        break;

      if (n >= 90)
        n = 0;
    }
  }
  printf("%d\\n", c + 1);
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  int N, T, H, L, t[100], h[100], c = 0, n = 0;
  scanf("%d%d%d%d", &N, &T, &H, &L);
  for (int i = 0; i < N; i++)
    scanf("%d%d", &t[i], &h[i]);
  while (1) {
    c = n % N;
    if (t[c] <= 0 && h[c] <= 0) {
      printf("%d\\n", c + 1);
      break;
    } else {
      if (t[c] == 0) {
        h[c] -= 1;
        t[c] += T + 10 - 9;
        T -= T + 10 - 9;
      } else {
        t[c]--;
        T++;
      }
    }
    if (T < 0 || T > L) {
      printf("%d\\n", c + 1);
      break;
    }
    if (T >= 9) {
      t[c] += T - 9;
      T -= T - 9;
    }
    n++;
  }
}
Problem p01532 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

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

void solve() {
  int player = -1;

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

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

void input() {

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

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

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

void solve() {
  int player = -1;

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

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

void input() {

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

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

int main() {
  input();
  solve();
  return 0;
}
Problem p01534 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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

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

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

Error: TLE

Fixed submission:

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

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

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

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

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

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