alexjercan/bugnet huggingface dataset:
Problem p01713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MAX_N 100000
#define INF 1234567890

int W;
int x[MAX_N];
int sum;
int loop;
int Left, Right;

int sum2;

int g;

int main() {
  cin >> W;
  for (int i = 0; i < W; i++) {
    cin >> x[i];
    sum2 += x[i];
  }
  if (sum2 == 999990000) {
    cout << "999990000" << endl;
    goto G;
  }
  for (int i = 0; i < W; i++) {
    Left = INF;
    Right = INF;
    if (x[i] > 0) {
      g = max(0, i - x[i] - 500);
      for (int j = i - 1; j >= g; j--) {
        if (x[j] < 0) {
          Left = min(Left, max(0, -x[j] - (i - j)));
        }
        if (x[j] == 0) {
          goto E;
        }
      }
      Left = 0;
    E:;
      g = min(W, i + x[i] + 500);
      for (int j = i + 1; j < g; j++) {
        if (x[j] < 0) {
          Right = min(Right, max(0, -x[j] - (j - i)));
        }
        if (x[j] == 0) {
          goto F;
        }
      }
      Right = 0;
    F:;
      sum += min(x[i], max(Left, Right));
    }
  }
  cout << sum << endl;
G:;
  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX_N 100000
#define INF 1234567890

int W;
int x[MAX_N];
int sum;
int loop;
int Left, Right;

int sum2;

int g;

int main() {
  cin >> W;
  for (int i = 0; i < W; i++) {
    cin >> x[i];
    sum2 += x[i];
  }
  if (sum2 == 999990000) {
    cout << "999990000" << endl;
    goto G;
  }
  for (int i = 0; i < W; i++) {
    Left = INF;
    Right = INF;
    if (x[i] > 0) {
      g = max(0, i - 11000);
      for (int j = i - 1; j >= g; j--) {
        if (x[j] < 0) {
          Left = min(Left, max(0, -x[j] - (i - j)));
        }
        if (x[j] == 0) {
          goto E;
        }
      }
      Left = 0;
    E:;
      g = min(W, i + 11000);
      for (int j = i + 1; j < g; j++) {
        if (x[j] < 0) {
          Right = min(Right, max(0, -x[j] - (j - i)));
        }
        if (x[j] == 0) {
          goto F;
        }
      }
      Right = 0;
    F:;
      sum += min(x[i], max(Left, Right));
    }
  }
  cout << sum << endl;
G:;
  return 0;
}
Problem p01713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF (1e9)
#define N 100005
using namespace std;
int n, a[N], l[N], r[N];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (a[i])
      continue;
    int x = INF;
    r[i] = x;
    for (int j = i + 1; j < n; j++) {
      if (a[j] < 0 && x > -a[j])
        x = -a[j];
      if (!a[j])
        x = INF;
      r[j] = x;
      if (x)
        x--;
    }
    break;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (a[i])
      continue;
    int x = INF;
    l[i] = x;
    for (int j = i - 1; j >= 0; j--) {
      if (a[j] < 0 && x > -a[j])
        x = -a[j];
      if (!a[j])
        x = INF;
      l[j] = x;
      if (x)
        x--;
    }
    break;
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (a[i] > 0)
      ans += min(a[i], max(l[i], r[i]));
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF (1e9)
#define N 100005
using namespace std;
int n, a[N], l[N], r[N];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (a[i])
      continue;
    int x = INF;
    r[i] = x;
    for (int j = i + 1; j < n; j++) {
      if (a[j] < 0 && x > -a[j])
        x = -a[j];
      if (!a[j])
        x = INF;
      r[j] = x;
      if (x)
        x--;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (a[i])
      continue;
    int x = INF;
    l[i] = x;
    for (int j = i - 1; j >= 0; j--) {
      if (a[j] < 0 && x > -a[j])
        x = -a[j];
      if (!a[j])
        x = INF;
      l[j] = x;
      if (x)
        x--;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (a[i] > 0)
      ans += min(a[i], max(l[i], r[i]));
  cout << ans << endl;
  return 0;
}
Problem p01713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = (n); i < (int)N; i++)
#define p(s) cout << (s) << endl
using namespace std;
const int inf = 1e9;

int tmp[100010];
int main() {
  int W, a[100010], ans = 0;
  cin >> W;
  REP(i, 0, W) cin >> a[i];

  int t = 0;
  REP(i, 0, W) {
    if (a[i] == 0)
      t = inf;
    else if (a[i] < 0)
      t = min(t, -a[i]);
    else
      tmp[i] = t;
    t--;
  }
  t = 0;
  for (int i = W - 1; i >= 0; i--) {
    if (a[i] == 0)
      t = inf;
    else if (a[i] < 0)
      t = min(t, -a[i]);
    else
      ans += max(0, min(a[i], max(tmp[i], t)));
    t--;
  }

  p(ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = (n); i < (int)N; i++)
#define p(s) cout << (s) << endl
using namespace std;
const int inf = 1e9;

int main() {
  int W, a[100010], ans = 0;
  cin >> W;
  REP(i, 0, W) cin >> a[i];

  vector<int> v, enter;
  v.push_back(-inf);
  REP(i, 0, W) {
    if (a[i] <= 0)
      v.push_back(i);
    else
      enter.push_back(i);
  }
  v.push_back(inf);
  for (auto in : enter) {
    int left, right;
    left = right = a[in];
    auto itr = upper_bound(v.begin(), v.end(), in);
    if (*itr != inf) {
      while (a[*itr] < 0) {
        right = min(right, max(0, -a[*itr] - (*itr - in)));
        itr++;
        if (*itr == inf) {
          right = 0;
          break;
        }
      }
    } else {
      right = 0;
    }
    itr = upper_bound(v.begin(), v.end(), in) - 1;
    if (*itr != -inf) {
      while (a[*itr] < 0) {
        left = min(left, max(0, -a[*itr] - (in - *itr)));
        itr--;
        if (*itr == -inf) {
          left = 0;
          break;
        }
      }
    } else {
      left = 0;
    }
    ans += max(left, right);
  }

  p(ans);

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

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

int s, n, m;
int x[2000];
int t, p;
map<int, int> mp;
vector<int> A, B;
int dp[2005][2005];

int main() {
  cin >> s >> n >> m;
  for (int i = 0; i < s; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++) {
    cin >> t >> p;
    mp[t - x[p - 1]]++;
  }

  A.push_back(0);
  B.push_back(0);
  map<int, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    A.push_back(it->first);
    B.push_back(it->second);
  }
  int size = A.size() - 1, base = A[1];
  for (int i = 1; i <= size; i++)
    A[i] -= base;
  /*
  for(int i=1;i<=size;i++)cout<<A[i]<<' '<<B[i]<<endl;
  cout<<endl;
  */

  fill(dp[0], dp[1], 0);
  fill(dp[1], dp[2005], 1e9);

  for (int j = 1; j <= m; j++) {
    for (int i = 1; i <= size; i++) {

      int sum = 0;
      for (int k = i - 1; k >= 0; k--) {
        dp[i][j] = min(dp[i][j], dp[k][j - 1] + sum);
        sum += (A[i] - A[k]) * B[k];
        if (sum > dp[i][j])
          break;
      }
    }
  }

  /*
  for(int j=1;j<9;j++){
    for(int i=1;i<9;i++){
      if(dp[i][j]==1e9)printf(" INF ");
      else printf("%4d ",dp[i][j]);
    }
    printf("\\n");
  }
  */
  cout << dp[size][m] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int s, n, m;
int x[2000];
int t, p;
map<int, int> mp;
vector<int> A, B;
int dp[2005][2005];

int main() {
  cin >> s >> n >> m;
  for (int i = 0; i < s; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++) {
    cin >> t >> p;
    mp[t - x[p - 1]]++;
  }

  A.push_back(0);
  B.push_back(0);
  map<int, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    A.push_back(it->first);
    B.push_back(it->second);
  }
  int size = A.size() - 1, base = A[1];
  for (int i = 1; i <= size; i++)
    A[i] -= base;

  // for(int i=1;i<=size;i++)cout<<A[i]<<' '<<B[i]<<endl;
  // cout<<endl;

  for (int i = 1; i < 2005; i++)
    for (int j = 0; j <= i; j++)
      dp[i][j] = 1e9;

  for (int i = 1; i <= size; i++) {
    for (int j = 1; j <= min(i + 1, m); j++) {
      int sum = 0;
      for (int k = i - 1; k >= 0; k--) {
        dp[i][j] = min(dp[i][j], dp[k][j - 1] + sum);
        sum += (A[i] - A[k]) * B[k];
      }
    }
  }
  /*
  for(int i=1;i<9;i++){
    for(int j=1;j<9;j++){
      if(dp[i][j]==1e9)printf(" INF ");
      else printf("%4d ",dp[i][j]);
    }
    printf("\\n");
  }
  */
  cout << dp[size][m] << endl;

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

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

int dp[2222][2222];
int R[2222][2222];
int S, N, M;
const int INF = (1000000009);

bool update(int &a, int b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int solvek() {
  fill(dp[0], dp[M + 1], INF);
  dp[0][0] = 0;
  for (int i = 1; i <= M; i++) {
    int kid = 0;
    for (int j = 1; j <= N; j++) {
      while (kid < j && update(dp[i][j], dp[i - 1][kid] + R[j][kid]))
        kid++;
      kid--;
      cout << kid << " ";
    }
    cout << endl;
    for (int j = 0; dp[i][j] != 0; j++)
      dp[i][j] = 0;
  }
  return dp[M][N];
}

int solve() {
  fill(dp[0], dp[M + 1], INF);
  dp[0][0] = 0;
  for (int i = 1; i <= M; i++) {
    int minik = 0;
    for (int j = 1; j <= N; j++) {
      for (int kid = minik; kid < N; kid++) {
        if (update(dp[i][j], dp[i - 1][kid] + R[j][kid])) {
          minik = max(minik, kid);
        }
      }
    }
    for (int j = 0; dp[i][j] != 0; j++)
      dp[i][j] = 0;
  }
  return dp[M][N];
}

int main() {
  cin >> S >> N >> M;
  int X[2222], Y[2222];
  for (int i = 0; i < S; i++)
    cin >> X[i];

  for (int i = 0; i < N; i++) {
    int t, p;
    cin >> t >> p;
    p--;
    Y[i] = t - X[p];
  }

  sort(Y, Y + N);

  for (int i = 0; i < N; i++) {
    int sum = 0;
    // cout << " y " << Y[i] << endl;
    for (int j = i; j > -1; j--) {
      sum += Y[i] - Y[j];
      R[i + 1][j] = sum;
      // cout << "[ " << j << ", " << i+1 << " ) = " << R[i+1][j] << endl;
    }
  }

  cout << solve() << endl;
}

Error: TLE

Fixed submission:

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

int dp[2222][2222];
int R[2222][2222];
int S, N, M;
const int INF = (1000000009);

bool update(int &a, int b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int solvek() {
  fill(dp[0], dp[M + 1], INF);
  dp[0][0] = 0;
  for (int i = 1; i <= M; i++) {
    int kid = 0;
    for (int j = 1; j <= N; j++) {
      while (kid < j && update(dp[i][j], dp[i - 1][kid] + R[j][kid]))
        kid++;
      kid--;
      cout << kid << " ";
    }
    cout << endl;
    for (int j = 0; dp[i][j] != 0; j++)
      dp[i][j] = 0;
  }
  return dp[M][N];
}

int solve() {
  fill(dp[0], dp[M + 1], INF);
  dp[0][0] = 0;
  for (int i = 1; i <= M; i++) {
    int minik = 0;
    for (int j = 1; j <= N; j++) {
      for (int kid = minik; kid < N; kid++) {
        if (update(dp[i][j], dp[i - 1][kid] + R[j][kid])) {
          minik = min(minik, kid);
        }
      }
    }
    for (int j = 0; dp[i][j] != 0; j++)
      dp[i][j] = 0;
  }
  return dp[M][N];
}

int main() {
  cin >> S >> N >> M;
  int X[2222], Y[2222];
  for (int i = 0; i < S; i++)
    cin >> X[i];

  for (int i = 0; i < N; i++) {
    int t, p;
    cin >> t >> p;
    p--;
    Y[i] = t - X[p];
  }

  sort(Y, Y + N);

  for (int i = 0; i < N; i++) {
    int sum = 0;
    // cout << " y " << Y[i] << endl;
    for (int j = i; j > -1; j--) {
      sum += Y[i] - Y[j];
      R[i + 1][j] = sum;
      // cout << "[ " << j << ", " << i+1 << " ) = " << R[i+1][j] << endl;
    }
  }

  cout << solve() << endl;
}
Problem p01715 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF (1 << 20)
#define EPS (1e-10)
#define EQ(a, b) (fabs((a) - (b)) < EPS)

#define MAX_N 2000
#define MAX_M 2000
#define MAX_S 2000
vector<vector<int>> cur(MAX_N, vector<int>(MAX_N, 0)),
    old(MAX_N, vector<int>(MAX_N, 0)); // 消去数のカウント
int main() {
  vector<int> tl;
  int xl[MAX_S + 1];
  int s, n, m;
  cin >> s >> n >> m;
  for (int i = 1; i <= s; ++i)
    cin >> xl[i];
  for (int i = 0; i < n; ++i) {
    int t, p;
    cin >> t >> p;
    tl.push_back(t - xl[p]);
  }
  sort(tl.begin(), tl.end());
  for (int i = 1; i < m; ++i) {       // 回
    for (int j = i - 1; j < n; ++j) { // まで
      if (j == 0)
        ++j;
      for (int k = j; k < n; ++k) { // 考慮範囲
        cur[j][k] =
            max(old[j - 1][j - 1] + (tl[k] - tl[j - 1]) * j, cur[j - 1][k]);
      }
    }
    cur.swap(old);
  }
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += tl[n - 1] - tl[i];
  }
  cout << sum - old[n - 1][n - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF (1 << 20)
#define EPS (1e-10)
#define EQ(a, b) (fabs((a) - (b)) < EPS)

#define MAX_N 2000
#define MAX_M 2000
#define MAX_S 2000
vector<vector<vector<int>>> dp(2, vector<vector<int>>(MAX_N,
                                                      vector<int>(MAX_N, 0)));
int main() {
  vector<int> tl;
  int xl[MAX_S + 1];
  int s, n, m;
  cin >> s >> n >> m;
  for (int i = 1; i <= s; ++i)
    cin >> xl[i];
  for (int i = 0; i < n; ++i) {
    int t, p;
    cin >> t >> p;
    tl.push_back(t - xl[p]);
  }
  sort(tl.begin(), tl.end());
  bool cur = 0;
  for (int i = 1; i < m; ++i) {     // 回
    for (int j = 1; j < n; ++j) {   // まで
      for (int k = j; k < n; ++k) { // 考慮範囲
        dp[cur][j][k] = max(dp[!cur][j - 1][j - 1] + (tl[k] - tl[j - 1]) * j,
                            dp[cur][j - 1][k]);
      }
    }
    cur = !cur;
  }
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += tl[n - 1] - tl[i];
  }
  cout << sum - dp[!cur][n - 1][n - 1] << endl;
  return 0;
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

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

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

void chmax(int &l, int r) {
  if (l < r)
    l = r;
}

int main() {

  int n, d, x;
  cin >> n >> d >> x;
  int p[d][n];
  rep(i, d) rep(j, n) cin >> p[i][j];

  // dp[j][k]:=i日目、種類jまで見て、k円持ってるときの、翌日の所持金の最大
  const int M = 100005;
  int dp[n + 1][M];
  rep(i, n + 1) rep(j, M) dp[i][j] = 0;
  rep(i, d - 1) {
    rep(i, n + 1) rep(j, M) dp[i][j] = 0;
    for (int k = 0; k <= x; k++)
      dp[0][k] = k; // ?
    rep(j, n) {
      for (int k = 0; k <= x; k++) {
        chmax(dp[j + 1][k], dp[j][k]);
        if (k - p[i][j] >= 0) {
          chmax(dp[j + 1][k], dp[j + 1][k - p[i][j]] + p[i + 1][j]);
        }
      }
    }
    int y = 0;
    for (int k = 0; k <= x; k++)
      chmax(y, dp[n][k]);
    x = y;
  }

  cout << x << endl;

  return 0;
}


Error: 0

Fixed submission:

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

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

void chmax(int &l, int r) {
  if (l < r)
    l = r;
}

int main() {

  int n, d, x;
  cin >> n >> d >> x;
  int p[d][n];
  rep(i, d) rep(j, n) cin >> p[i][j];

  // dp[j][k]:=i日目、種類jまで見て、k円持ってるときの、翌日の所持金の最大
  const int M = 100;
  int dp[n + 1][M];
  rep(i, n + 1) rep(j, M) dp[i][j] = 0;
  rep(i, d - 1) {
    for (int k = 0; k <= x; k++)
      dp[0][k] = k; // ?
    rep(j, n) {
      for (int k = 0; k <= x; k++) {
        chmax(dp[j + 1][k], dp[j][k]);
        if (k - p[i][j] >= 0) {
          chmax(dp[j + 1][k], dp[j + 1][k - p[i][j]] + p[i + 1][j]);
        }
      }
    }
    int y = x;
    for (int k = 0; k <= x; k++)
      chmax(y, dp[n][k]);
    x = y;
  }

  cout << x << endl;

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

#include <iostream>
using namespace std;
int n, p[10][10], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I + 1; i < n; i++)
    for (int j = 1; (p[day][i] / p[day - 1][i]) && (x / (p[day - 1][i] * j));
         j++) {
      f = 0;
      saiki(sum + p[day][i] * j, x - p[day - 1][i] * j, day, i);
    }
  if (f)
    ans = max(ans, sum + x);
}

int main() {
  int d, x;
  cin >> n >> d >> x;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  ans = x;
  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, -1);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, p[10][10], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I + 1; i < n; i++)
    for (int j = 1; x / (p[day - 1][i] * j); j++)
      f = 0, saiki(sum + p[day][i] * j, x - p[day - 1][i] * j, day, i);

  if (f)
    ans = max(ans, sum + x);
}

int main() {
  int d, x;
  cin >> n >> d >> x;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  ans = x;
  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, -1);
  cout << ans << endl;
  return 0;
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

struct Item {
  int value, weight;
};

int dp[11][100001];
int knapsack(vector<Item> &items, int W) {
  fill(dp[0], dp[11], -1);
  dp[0][0] = 0;
  int N = items.size();
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (dp[i][j] < 0)
        continue;
      int nj = j + items[i].weight;
      if (nj <= W) {
        dp[i][nj] = max(dp[i][nj], dp[i][j] + items[i].value);
      }
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }
  int res = 0;
  for (int i = 0; i <= W; ++i) {
    res = max(res, dp[N][i]);
  }
  return res;
}

int main() {
  int N, D, X;
  cin >> N >> D >> X;
  vector<vector<int>> P(D, vector<int>(N));
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> P[i][j];
    }
  }
  vector<vector<Item>> items(D - 1, vector<Item>(N));
  for (int i = 0; i < D - 1; ++i) {
    for (int j = 0; j < N; ++j) {
      items[i][j] = (Item){P[i + 1][j] - P[i][j], P[i][j]};
    }
  }
  for (int i = 0; i < items.size(); ++i) {
    X += knapsack(items[i], X);
  }
  cout << X << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Item {
  int value, weight;
};

int dp[11][10001];
int knapsack(vector<Item> &items, int W) {
  fill(dp[0], dp[11], -1);
  dp[0][0] = 0;
  int N = items.size();
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (dp[i][j] < 0)
        continue;
      int nj = j + items[i].weight;
      if (nj <= W) {
        dp[i][nj] = max(dp[i][nj], dp[i][j] + items[i].value);
      }
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }
  int res = 0;
  for (int i = 0; i <= W; ++i) {
    res = max(res, dp[N][i]);
  }
  return res;
}

int main() {
  int N, D, X;
  cin >> N >> D >> X;
  vector<vector<int>> P(D, vector<int>(N));
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> P[i][j];
    }
  }
  vector<vector<Item>> items(D - 1, vector<Item>(N));
  for (int i = 0; i < D - 1; ++i) {
    for (int j = 0; j < N; ++j) {
      items[i][j] = (Item){P[i + 1][j] - P[i][j], P[i][j]};
    }
  }
  for (int i = 0; i < items.size(); ++i) {
    X += knapsack(items[i], X);
  }
  cout << X << endl;
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int i, j, k, n, d, x, maxx, p[11][11], curp, nextp;

  cin >> n >> d >> x;
  for (i = 0; i < d; i++) {
    for (j = 0; j < n; j++) {
      cin >> p[i][j];
    }
  }

  for (i = 0; i < d - 1; i++) {
    int dp[100000] = {};
    for (j = 0; j < n; j++) {
      if (1.0 * p[i + 1][j] / p[i][j] <= 1.0)
        continue;
      curp = p[i][j];
      nextp = p[i + 1][j];
      k = 0;
      while (k + curp <= x) {
        dp[k + curp] = max(dp[k + curp], dp[k] + nextp);
        k++;
      }
    }
    maxx = x;
    for (k = 0; k <= x; k++) {
      maxx = max(maxx, dp[k] + x - k);
    }
    x = maxx;
  }

  cout << x << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int i, j, k, n, d, x, maxx, p[12][12], curp, nextp;

  cin >> n >> d >> x;
  for (i = 0; i < d; i++) {
    for (j = 0; j < n; j++) {
      cin >> p[i][j];
    }
  }

  for (i = 0; i < d - 1; i++) {
    int dp[10001] = {};
    for (j = 0; j < n; j++) {
      if (1.0 * p[i + 1][j] / p[i][j] <= 1.0)
        continue;
      curp = p[i][j];
      nextp = p[i + 1][j];
      k = 0;
      while (k + curp <= x) {
        dp[k + curp] = max(dp[k + curp], dp[k] + nextp);
        k++;
      }
    }
    maxx = x;
    for (k = 0; k <= x; k++) {
      maxx = max(maxx, dp[k] + x - k);
    }
    x = maxx;
  }

  cout << x << endl;

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

class in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    int operator*() { return it; }
    bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr in(int n) : i(0), n(n) {}
  explicit constexpr in(int i, int n) : i(i, n < i), n(n) {}
  const my_iterator &begin() { return i; }
  const my_iterator &end() { return n; }
};

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, d, x;
  cin >> n >> d >> x;
  vector<vector<int>> p(d, vector<int>(n));
  for (auto &v : p)
    for (auto &a : v)
      cin >> a;
  for (int i : in(d - 1)) {
    vector<int> dp(x + 1, 0);
    for (int j : in(n))
      if (p[i][j] <= x)
        for (int k : in(p[i][j], x + 1))
          dp[k] = max(dp[k], dp[k - p[i][j]] + p[i + 1][j] - p[i][j]);
    x += dp[x];
  }
  cout << x << endl;
  return 0;
}

Error: 0

Fixed submission:

class in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    int operator*() { return it; }
    bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr in(int n) : i(0), n(n) {}
  explicit constexpr in(int i, int n) : i(i, n < i), n(n) {}
  const my_iterator &begin() { return i; }
  const my_iterator &end() { return n; }
};

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, d, x;
  cin >> n >> d >> x;
  vector<vector<int>> p(d, vector<int>(n));
  for (auto &v : p)
    for (auto &a : v)
      cin >> a;
  for (int i : in(d - 1)) {
    vector<int> dp(x + 1, 0);
    for (int j : in(n))
      for (int k : in(p[i][j], x + 1))
        dp[k] = max(dp[k], dp[k - p[i][j]] + p[i + 1][j] - p[i][j]);
    x += dp[x];
  }
  cout << x << endl;
  return 0;
}