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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10000000;

int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (x >= A[j]) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
void quicksort(int A[], int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int getNumberOfCoin(int C[], int n, int m) {
  int T[50000];
  for (int j = 0; j <= n; j++) {
    T[j] = INF;
  }
  T[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - C[i] >= 0) {
        T[j] = min(T[j], T[j - C[i]] + 1);
      }
    }
  }
  return T[n];
}
int knapsack(int w, int n, int v[], int ww[]) {
  int W[100][10000];
  for (int j = 0; j <= w; j++) {
    W[0][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (j - ww[i] < 0) {
        W[i + 1][j] = W[i][j];
      }
      if (j - ww[i] >= 0) {
        W[i + 1][j] = max(W[i][j], W[i][j - ww[i]] + v[i]);
      }
    }
  }
  return W[n][w];
}
int main() {
  int N, W;
  cin >> N >> W;
  int v[100];
  int w[100];
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << knapsack(W, N, v, w) << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10000000;

int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (x >= A[j]) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
void quicksort(int A[], int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int getNumberOfCoin(int C[], int n, int m) {
  int T[50000];
  for (int j = 0; j <= n; j++) {
    T[j] = INF;
  }
  T[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - C[i] >= 0) {
        T[j] = min(T[j], T[j - C[i]] + 1);
      }
    }
  }
  return T[n];
}
int knapsack(int w, int n, int v[], int ww[]) {
  int W[101][10001];
  for (int j = 0; j <= w; j++) {
    W[0][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (j - ww[i] < 0) {
        W[i + 1][j] = W[i][j];
      }
      if (j - ww[i] >= 0) {
        W[i + 1][j] = max(W[i][j], W[i][j - ww[i]] + v[i]);
      }
    }
  }
  return W[n][w];
}
int main() {
  int N, W;
  cin >> N >> W;
  int v[100];
  int w[100];
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << knapsack(W, N, v, w) << endl;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  int value[N];
  int weight[N];
  REP(n, N) {
    cin >> value[n];
    cin >> weight[n];
  }

  int dp[100][10001] = {{}};

  REP(n, N) {
    REP(w, W + 1) {
      weight[n] > w
          ? dp[n + 1][w] = dp[n][w]
          : dp[n + 1][w] = max(dp[n][w], dp[n][w - weight[n]] + value[n]);
    }
  }

  cout << dp[N][W] << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  int value[N];
  int weight[N];
  REP(n, N) {
    cin >> value[n];
    cin >> weight[n];
  }

  int dp[101][10002] = {{}};

  REP(n, N) {
    REP(w, W + 1) {
      weight[n] > w
          ? dp[n + 1][w] = dp[n][w]
          : dp[n + 1][w] = max(dp[n][w], dp[n][w - weight[n]] + value[n]);
    }
  }

  cout << dp[N][W] << endl;

  return 0;
}
Problem p02316 (C++)

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

#define repeat(i, n) for (int i = 0; i < (n); ++i)
#define repeat_rev(i, n) for (int i = (n)-1; 0 <= i; ++i)

#define MAX_N 100
#define MAX_W 10000

struct item {
  int v, w;
};
int N, W;
item t[MAX_N];
int dp[2][MAX_W + 1];

int main() {
  cin >> N >> W;
  repeat(i, N) cin >> t[i].v >> t[i].w;
  int *cur = dp[0];
  fill(cur, cur + W + 1, 0);
  int *prv = dp[1];
  repeat(n, N) {
    swap(cur, prv);
    repeat(w, W + 1) cur[w] =
        max(prv[w], (0 <= w - t[n].w ? cur[w - t[n].w] + t[n].v : 0));
    repeat(w, W + 1) cerr << cur[w] << ' ';
    cerr << endl;
  }
  repeat_rev(i, W + 1) if (cur[i]) {
    cout << cur[i] << endl;
    break;
  }
  return 0;
}

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

#define repeat(i, n) for (int i = 0; i < (n); ++i)
#define repeat_rev(i, n) for (int i = (n)-1; 0 <= i; ++i)

#define MAX_N 100
#define MAX_W 10000

struct item {
  int v, w;
};
int N, W;
item t[MAX_N];
int dp[2][MAX_W + 1];

int main() {
  cin >> N >> W;
  repeat(i, N) cin >> t[i].v >> t[i].w;
  int *cur = dp[0];
  fill(cur, cur + W + 1, 0);
  int *prv = dp[1];
  repeat(n, N) {
    swap(cur, prv);
    repeat(w, W + 1) cur[w] =
        max(prv[w], (0 <= w - t[n].w ? cur[w - t[n].w] + t[n].v : 0));
  }
  repeat_rev(i, W + 1) if (cur[i]) {
    cout << cur[i] << endl;
    break;
  }
  return 0;
}
Problem p02316 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int i, j, k, a, b, c[1001], d[1001], e[10001], f = -1;
int main(void) {
  scanf("%d %d", &a, &b);
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      e[i] = -1;
    }
  }
  for (i = 0; i < a; i++) {
    scanf("%d %d", &c[i], &d[i]);
  }
  e[0] = 0;
  for (i = 0; i <= b; i++) {
    if (e[i] != -1) {
      for (j = 0; j <= a; j++) {
        if (e[i + d[j]] < e[i] + c[j]) {
          e[i + d[j]] = e[i] + c[j];
        }
      }
    }
  }
  for (i = 0; i <= b; i++) {
    if (f < e[i]) {
      f = e[i];
    }
  }
  printf("%d\n", f);
  return 0;
}

Fixed submission:
#include <stdio.h>
int i, j, k, a, b, c[1001], d[1001], e[10001], f = -1;
int main(void) {
  scanf("%d %d", &a, &b);
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      e[i] = -1;
    }
  }
  for (i = 0; i < a; i++) {
    scanf("%d %d", &c[i], &d[i]);
  }
  e[0] = 0;
  for (i = 0; i <= b; i++) {
    if (e[i] != -1) {
      for (j = 0; j <= a; j++) {
        if (e[i + d[j]] < e[i] + c[j] && i + d[j] <= b) {
          e[i + d[j]] = e[i] + c[j];
        }
      }
    }
  }
  for (i = 0; i <= b; i++) {
    if (f < e[i]) {
      f = e[i];
    }
  }
  printf("%d\n", f);
  return 0;
}
Problem p02316 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int N, W, value[101], weight[101], dp[101][10001];

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> value[i] >> weight[i];
  }
  for (int i = 0; i < N; ++i) {
    for (int w = 0; w <= W; ++w) {
      for (int k = 0; k * weight[i] <= w; ++k) {
        dp[i + 1][w] =
            max(dp[i + 1][w], dp[i][w - k * weight[i]] + k * value[i]);
      }
    }
  }
  cout << dp[N][W] << endl;
}


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

using namespace std;

int N, W, value[101], weight[101], dp[101][10001];

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> value[i] >> weight[i];
  }
  for (int i = 0; i < N; ++i) {
    for (int w = 0; w <= W; ++w) {
      if (w >= weight[i]) {
        dp[i + 1][w] = max(dp[i][w], dp[i + 1][w - weight[i]] + value[i]);
      } else {
        dp[i + 1][w] = dp[i][w];
      }
    }
  }
  cout << dp[N][W] << endl;
}
Problem p02316 (C++)

Buggy submission (status: Time Limit Exceeded):
#if 0

#endif

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

int N, W;
int v[110];
int w[110];
int V[110][10010];

int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  for (int i = 0; i <= N; i++) {
    for (int c = 0; c <= W; c++) {
      if (i == 0 || c == 0)
        V[i][c] = 0;
      else if (c - w[i] < 0)
        V[i][c] = V[i - 1][c];
      else {
        int n = 1;
        int t = 0;
        while (c - w[i] * n >= 0) {
          t = max(t, v[i] * n + V[i - 1][c - w[i] * n]);
          n++;
        }
        V[i][c] = max(t, V[i - 1][c]);
      }
    }
  }
  cout << V[N][W] << endl;
}


Fixed submission:
#if 0

#endif

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

int N, W;
int v[110];
int w[110];
int V[110][10010];

int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  for (int i = 0; i <= N; i++) {
    for (int c = 0; c <= W; c++) {
      if (i == 0 || c == 0)
        V[i][c] = 0;
      else if (c - w[i] < 0)
        V[i][c] = V[i - 1][c];
      else {
        int a[3] = {v[i] + V[i][c - w[i]], v[i] + V[i - 1][c - w[i]],
                    V[i - 1][c]};
        V[i][c] = *max_element(a, a + 3);
      }
    }
  }
  cout << V[N][W] << endl;
}
Problem p02316 (C++)

Buggy submission (status: Runtime Error):
/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_C&lang=jp
*/
#include <iostream>
using namespace std;

int v[1001], w[1001];
int n, k;
int dp[101][10001];
/*
ナップサック
iは品物の番号、jはその時の重さ
*/
void knapsack() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      if (w[i] > j) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> v[i] >> w[i];
  }
  knapsack();
  cout << dp[n][k] << "\n";
}


Fixed submission:
/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_C&lang=jp
*/
#include <iostream>
using namespace std;

int v[1001], w[1001];
int n, k;
int dp[101][10001];
/*
ナップサック
iは品物の番号、jはその時の重さ
*/
void knapsack() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (w[i] > j) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> v[i] >> w[i];
  }
  knapsack();
  cout << dp[n][k] << "\n";
}
Problem p02316 (C++)

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

using namespace std;

int n, w_max;
int dp[100][10000];
bool f[100][100000];
vector<pair<int, int>> item(101);

int saiki(int i, int j) {
  if (f[i][j]) {
    return dp[i][j];
  }

  int res;
  if (i == n) {
    res = 0;
  } else if (j < item[i].second) {
    res = saiki(i + 1, j);
  } else {
    res = max(saiki(i + 1, j), saiki(i, j - item[i].second) + item[i].first);
  }

  dp[i][j] = res;
  f[i][j] = true;
  return res;
}

int main() {
  cin >> n >> w_max;
  for (int i = 0; i < n; i++) {
    cin >> item[i].first >> item[i].second;
  }

  cout << saiki(0, w_max) << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

int n, w_max;
int dp[101][10001];
bool f[101][100001];
vector<pair<int, int>> item(101);

int saiki(int i, int j) {
  if (f[i][j]) {
    return dp[i][j];
  }

  int res;
  if (i == n) {
    res = 0;
  } else if (j < item[i].second) {
    res = saiki(i + 1, j);
  } else {
    res = max(saiki(i + 1, j), saiki(i, j - item[i].second) + item[i].first);
  }

  dp[i][j] = res;
  f[i][j] = true;
  return res;
}

int main() {
  cin >> n >> w_max;
  for (int i = 0; i < n; i++) {
    cin >> item[i].first >> item[i].second;
  }

  cout << saiki(0, w_max) << endl;

  return 0;
}
Problem p02316 (C++)

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

signed main() {
  int a, b;
  cin >> a >> b;
  int dp[1001]{};
  for (int c = 0; c < a; c++) {
    int d, e;
    cin >> d >> e;
    for (int f = e; f <= b; f++) {
      dp[f] = max(dp[f], dp[f - e] + d);
    }
  }
  cout << dp[b] << endl;
}

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

signed main() {
  int a, b;
  cin >> a >> b;
  int dp[10001]{};
  for (int c = 0; c < a; c++) {
    int d, e;
    cin >> d >> e;
    for (int f = e; f <= b; f++) {
      dp[f] = max(dp[f], dp[f - e] + d);
    }
  }
  cout << dp[b] << endl;
}
Problem p02316 (C++)

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

using namespace std;

int value[100], weight[100];
int table[101][10001] = {};

int main() {
  int N, W;
  cin >> N >> W;
  for (int i = 0; i < N; ++i) {
    cin >> value[i] >> weight[i];
  }

  for (int i = 1; i <= N; ++i) {
    for (int w = 0; w <= W; ++w) {
      table[i][w] = table[i - 1][w];
      for (int n = 1; w >= n * weight[i - 1]; ++n) {
        table[i][w] = max(table[i][w], n * value[i - 1] +
                                           table[i - 1][w - n * weight[i - 1]]);
      }
    }
  }

  cout << table[N][W] << endl;
  return 0;
}

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

using namespace std;

int value[100], weight[100];
int table[101][10001] = {};

int main() {
  int N, W;
  cin >> N >> W;
  for (int i = 0; i < N; ++i) {
    cin >> value[i] >> weight[i];
  }

  for (int i = 1; i <= N; ++i) {
    for (int w = 0; w <= W; ++w) {
      if (w >= weight[i - 1])
        table[i][w] =
            max(table[i - 1][w], value[i - 1] + table[i][w - weight[i - 1]]);
      else
        table[i][w] = table[i - 1][w];
    }
  }

  cout << table[N][W] << endl;
  return 0;
}
Problem p02316 (C++)

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

int dp[101][10001];

int main() {
  int n, w;
  scanf("%d %d\n", &n, &w);
  int val[n], wei[n];
  for (int i = 1; i <= n; i++) {
    scanf("%d %d\n", &val[i], &wei[i]);
  }
  // init. dp table
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int j = 0; j <= w; j++) {
    dp[0][j] = 0;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (wei[i] <= j) {
        dp[i][j] = std::max(dp[i - 1][j - wei[i]] + val[i], dp[i - 1][j]);
        for (int k = 2;; k++) {
          if (k * wei[i] <= j) {
            dp[i][j] =
                std::max(dp[i - 1][j - k * wei[i]] + k * val[i], dp[i][j]);
          } else {
            break;
          }
        }
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

  printf("%d\n", dp[n][w]);
  /*
      for(int i=0; i<=n; i++){
          for(int j=0; j<=w; j++){
              printf("%d ",dp[i][j]);
          }
          printf("\n");
      }
  */

  return 0;
}

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

int dp[101][10001];

int main() {
  int n, w;
  scanf("%d %d\n", &n, &w);
  int val[n], wei[n];
  for (int i = 1; i <= n; i++) {
    scanf("%d %d\n", &val[i], &wei[i]);
  }
  // init. dp table
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int j = 0; j <= w; j++) {
    dp[0][j] = 0;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (wei[i] <= j) {
        dp[i][j] = std::max(dp[i][j - wei[i]] + val[i], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

  printf("%d\n", dp[n][w]);
  /*
      for(int i=0; i<=n; i++){
          for(int j=0; j<=w; j++){
              printf("%d ",dp[i][j]);
          }
          printf("\n");
      }
  */

  return 0;
}
Problem p02316 (C++)

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

using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  int item[101][2];
  item[0][0] = 0;
  item[0][1] = 0;
  for (int i = 1; i < N + 1; ++i) {
    int v, w;
    cin >> v >> w;
    item[i][0] = v;
    item[i][1] = w;
  }
  int c[101][10001];
  for (int i = 0; i < W + 1; i++) {
    c[0][i] = 0;
  }
  for (int j = 0; j < N + 1; j++) {
    c[j][0] = 0;
  }
  for (int i = 1; i < N + 1; ++i) {
    int v = item[i][0];
    int w = item[i][1];
    for (int j = 1; j < W + 1; ++j) {
      if (j < w) {
        c[i][j] = c[i - 1][j];
      } else {
        int tmp = c[i - 1][j];
        int max_n = j / w;
        for (int k = 1; k < max_n + 1; ++k) {
          if (tmp < c[i - 1][j - w * k] + v * k) {
            tmp = c[i - 1][j - w * k] + v * k;
          }
        }
        c[i][j] = tmp;
      }
    }
  }
  cout << c[N][W] << endl;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  int item[101][2];
  item[0][0] = 0;
  item[0][1] = 0;
  for (int i = 1; i < N + 1; ++i) {
    int v, w;
    cin >> v >> w;
    item[i][0] = v;
    item[i][1] = w;
  }
  int c[101][10001];
  for (int i = 0; i < W + 1; i++) {
    c[0][i] = 0;
  }
  for (int j = 0; j < N + 1; j++) {
    c[j][0] = 0;
  }
  for (int i = 1; i < N + 1; ++i) {
    int v = item[i][0];
    int w = item[i][1];
    for (int j = 1; j < W + 1; ++j) {
      if (j < w) {
        c[i][j] = c[i - 1][j];
      } else {
        c[i][j] = max(c[i - 1][j], c[i][j - w] + v);
      }
    }
  }
  cout << c[N][W] << endl;
}
Problem p02316 (C++)

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

using namespace std;

int val[110], weight[10010];
int res[110][10010];

int main() {
  int N, W;
  cin >> N >> W;

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

  // res[number][weight]
  for (int i = 0; i < N + 1; i++) {
    res[i][0] = 0;
  }
  for (int i = 0; i < W + 1; i++) {
    res[0][i] = 0;
  }

  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < W + 1; j++) {

      if (weight[i - 1] > j) {

        res[i][j] = res[i - 1][j];

      } else {
        int resmax = 0;
        for (int l = 1; weight[i - 1] * l < j + 1; l++)
          if (resmax < max((val[i - 1] * l + res[i - l][j - weight[i - 1] * l]),
                           res[i - 1][j])) {

            resmax = max((val[i - 1] * l + res[i - l][j - weight[i - 1] * l]),
                         res[i - 1][j]);
          }
        res[i][j] = resmax;
      }
    }
  }
  // for(int j=1;j<W+1;j++) {
  //	cout << endl;
  //	for(int i=1;i<N+1;i++) {
  //		cout << res[i][j] << " ";
  //	}
  // }

  cout << res[N][W] << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int val[110], weight[10010];
int res[110][10010];

int main() {
  int N, W;
  cin >> N >> W;

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

  // res[number][weight]
  for (int i = 0; i < N + 1; i++) {
    res[i][0] = 0;
  }
  for (int i = 0; i < W + 1; i++) {
    res[0][i] = 0;
  }

  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < W + 1; j++) {

      if (weight[i - 1] > j) {

        res[i][j] = res[i - 1][j];

      } else {
        res[i][j] = max(res[i - 1][j], val[i - 1] + res[i][j - weight[i - 1]]);
      }
    }
  }
  // for(int j=1;j<W+1;j++) {
  //	cout << endl;
  //	for(int i=1;i<N+1;i++) {
  //		cout << res[i][j] << " ";
  //	}
  // }

  cout << res[N][W] << endl;
  return 0;
}
Problem p02316 (C++)

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

int main() {
  int n, ws;
  cin >> n >> ws;
  int v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

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

  for (int k = 1; k <= n; k++) {
    for (int i = 0; i <= ws; i++) {
      int temp = 0, j = 0;
      while (i >= w[k] * j) {
        dp[k][i] = max(dp[k][i], dp[k - 1][i - w[k] * j] + v[k] * j);
        j++;
      }
    }
  }

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


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

int main() {
  int n, ws;
  cin >> n >> ws;
  int v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

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

  for (int k = 1; k <= n; k++) {
    for (int i = 0; i <= ws; i++) {
      if (i < w[k])
        dp[k][i] = dp[k - 1][i];
      else
        dp[k][i] = max(dp[k - 1][i], dp[k][i - w[k]] + v[k]);
    }
  }

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

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

using namespace std;
typedef unsigned int ui;
// using ui = unsigned int;

int main(void) {
  string line;
  ui max_n, max_w;
  cin >> max_n >> max_w;
  ui v, w;

  vector<ui> table(max_w + 1, 0);

  for (int i = 1; i <= max_n; ++i) {
    cin >> v >> w;
    ui dv = v, dw = w;
    for (; w <= max_w; v += dv, w += dw) {
      for (int j = max_w - w; j >= 1; --j) {
        if (table[j] != 0 && table[w + j] < table[j] + v)
          table[w + j] = table[j] + v;
      }
      if (w <= max_w && table[w] < v)
        table[w] = v;
    }
  }
  cout << *(max_element(begin(table), end(table))) << endl;

  return 0;
}

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

using namespace std;
typedef unsigned int ui;
// using ui = unsigned int;

int main(void) {
  string line;
  ui max_n, max_w;
  cin >> max_n >> max_w;
  ui v, w;

  vector<ui> table(max_w + 1, 0);

  for (int i = 1; i <= max_n; ++i) {
    cin >> v >> w;
    if (w <= max_w && table[w] < v)
      table[w] = v;
    for (int j = 1; j <= max_w - w; j++) {
      if (table[j] != 0 && table[w + j] < table[j] + v)
        table[w + j] = table[j] + v;
    }
  }
  cout << *(max_element(begin(table), end(table))) << endl;

  return 0;
}
Problem p02316 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
typedef pair<ll, ll> ll_pair;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
const int N = 100;
const int W = 10000;
int n, w, v[N], wi[N];
int dp[N + 1][W + 1];

int rec(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  int res;
  if (i == n) {
    res = 0;
  } else if (j < wi[i]) {
    res = rec(i + 1, j);
  } else {
    int a = rec(i + 1, j);
    int b = rec(i + 1, j - wi[i]) + v[i];
    int k = 2;
    while (j - wi[i] * k >= 0) {
      int c = rec(i + 1, j - wi[i] * k) + v[i] * k;
      if (c > b) {
        b = c;
      }
      k++;
    }
    res = max(a, b);
  }
  return dp[i][j] = res;
}

int main() {
  FOR(i, 0, N + 1) FOR(j, 0, W + 1) dp[i][j] = -1;
  cin >> n >> w;
  FOR(i, 0, n) { cin >> v[i] >> wi[i]; }
  cout << rec(0, w) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
typedef pair<ll, ll> ll_pair;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
const int N = 100;
const int W = 10000;
int n, w, v[N], wi[N];
int dp[N + 1][W + 1];

int rec(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  int res;
  if (i == n) {
    res = 0;
  } else if (j < wi[i]) {
    res = rec(i + 1, j);
  } else {
    int a = rec(i + 1, j);
    int b = rec(i, j - wi[i]) + v[i];
    res = max(a, b);
  }
  return dp[i][j] = res;
}

int main() {
  FOR(i, 0, N + 1) FOR(j, 0, W + 1) dp[i][j] = -1;
  cin >> n >> w;
  FOR(i, 0, n) { cin >> v[i] >> wi[i]; }
  cout << rec(0, w) << endl;
  return 0;
}
Problem p02316 (C++)

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

long long calc(int i, int c, const vector<int> &v, const vector<int> &w,
               vector<vector<long long>> &ans) {
  if (i <= -1 || c < 0) {
    return 0;
  } else if (c == 0) {
    ans[i][c] = 0;
  } else if (ans[i][c] >= 0) {
    return ans[i][c];
  } else if (c - w[i] < 0) {
    ans[i][c] = calc(i - 1, c, v, w, ans);
  } else {
    int p = 1;
    long long q = -1;
    while (c - p * w[i] >= 0) {
      q = max(q, p * v[i] + calc(i - 1, c - p * w[i], v, w, ans));
      p++;
    }
    ans[i][c] = max(q, calc(i - 1, c, v, w, ans));

    // ans[i][c] = max(
    //   v[i] + calc(i-1, c - w[i], v, w, ans),
    //   calc(i-1, c, v, w, ans)
    // );
  }
  return ans[i][c];
}

int main() {
  int n, c;
  cin >> n >> c;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];
  vector<vector<long long>> ans(n, vector<long long>(c + 1, -1));
  cout << calc(n - 1, c, v, w, ans) << endl;
  return 0;
}

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

long long calc(int i, int c, const vector<int> &v, const vector<int> &w,
               vector<vector<long long>> &ans) {
  if (i <= -1 || c < 0) {
    return 0;
  } else if (c == 0) {
    ans[i][c] = 0;
  } else if (ans[i][c] >= 0) {
    return ans[i][c];
  } else if (c - w[i] < 0) {
    ans[i][c] = calc(i - 1, c, v, w, ans);
  } else {
    // int p = 1;
    // long long q = -1;
    // while(c - p*w[i] >= 0){
    //   q = max(
    //     q,
    //     p*v[i] + calc(i-1, c - p*w[i], v, w, ans)
    //   );
    //   p++;
    // }
    ans[i][c] =
        max(v[i] + calc(i, c - w[i], v, w, ans), calc(i - 1, c, v, w, ans));

    // ans[i][c] = max(
    //   v[i] + calc(i-1, c - w[i], v, w, ans),
    //   calc(i-1, c, v, w, ans)
    // );
  }
  return ans[i][c];
}

int main() {
  int n, c;
  cin >> n >> c;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];
  vector<vector<long long>> ans(n, vector<long long>(c + 1, -1));
  cout << calc(n - 1, c, v, w, ans) << endl;
  return 0;
}
Problem p02316 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int dp[10100];
int main(void) {
  int n, w;
  scanf("%d %d", &n, &w);
  int i, j;
  for (i = 0; i <= w; i++) {
    dp[i] = -1;
  }
  dp[0] = 0;
  for (i = 0; i < n; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    for (j = 0; j <= w; j++) {
      if (dp[j] >= 0 && dp[j + b] < dp[j] + a && j + b <= w) {
        dp[j + b] = dp[j] + a;
      }
    }
  }
  int max = 0;
  for (i = 0; i <= w; i++) {
    if (dp[i] > max) {
      max = dp[i];
    }
  }
  printf("%d\n", max);
  return 0;
}

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define p pair<int, int>
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }

int main() {
  int n;
  cin >> n;
  int weight;
  cin >> weight;
  vector<int> w(n), v(n);
  rep(i, n) { cin >> v[i] >> w[i]; }

  vector<vector<int>> a(n);
  rep(i, n) {
    a[i].resize(weight + 1);
    rep(j, weight + 1) {
      if (i == 0) {
        a[0][j] = (j / w[0]) * v[0];
        /*
        if(j>=w[0])a[0][j]=v[0];
        else a[0][j]=0;
        */
      } else {
        int k = 0;
        int m = a[i - 1][j];
        while (j - k * w[i] >= 0) {
          m = max(m, a[i - 1][j - k * w[i]] + k * v[i]);
          k++;
        }
        a[i][j] = m;
        /*
        if(j<w[i]) a[i][j]=a[i-1][j];
        else a[i][j]=max(a[i-1][j],a[i-1][j-w[i]]+v[i]);
        */
      }
    }
  }
  cout << a[n - 1][weight] << endl;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define p pair<int, int>
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }

int main() {
  int n;
  cin >> n;
  int weight;
  cin >> weight;
  vector<int> w(n), v(n);
  rep(i, n) { cin >> v[i] >> w[i]; }

  vector<vector<int>> a(n);
  rep(i, n) {
    a[i].resize(weight + 1);
    rep(j, weight + 1) {
      if (i == 0) {
        a[0][j] = (j / w[0]) * v[0];
        /*
        if(j>=w[0])a[0][j]=v[0];
        else a[0][j]=0;
        */
      } else {
        if (j < w[i])
          a[i][j] = a[i - 1][j];
        else
          a[i][j] = max(a[i - 1][j],
                        max(a[i - 1][j - w[i]] + v[i], a[i][j - w[i]] + v[i]));
      }
    }
  }
  cout << a[n - 1][weight] << endl;
}
Problem p02316 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n, w;
  struct {
    int v, w;
  } item[101];
  cin >> n >> w;
  for (int i = 0; i <= n; i++)
    cin >> item[i].v >> item[i].w;
  int dp[101][10001] = {};
  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= w; j++)
      if (item[i].w <= j)
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - item[i].w] + item[i].v);
      else
        dp[i + 1][j] = dp[i][j];
  cout << dp[n][w] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n, w;
  struct {
    int v, w;
  } item[101];
  cin >> n >> w;
  for (int i = 0; i <= n; i++)
    cin >> item[i].v >> item[i].w;
  int dp[101][10001] = {};
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= w; j++)
      if (item[i].w <= j)
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - item[i].w] + item[i].v);
      else
        dp[i + 1][j] = dp[i][j];
  cout << dp[n][w] << endl;
  return 0;
}
Problem p02316 (C++)

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

using namespace std;

#define REP(var, count) for (int var = 0; var < count; var++)

int DP[110][10010]; // N???????????§????????????????¨?w

int main(void) {

  int N, W;
  cin >> N >> W;

  REP(i, N) {
    int v, w;
    cin >> v >> w;

    REP(c, W + 1) {
      DP[i + 1][c] = DP[i][c];
      for (int cnt = 1; cnt <= c / w; cnt++) {
        DP[i + 1][c] = max(DP[i][c - w * cnt] + v * cnt, DP[i + 1][c]);
      }
    }
  }

  int ret = 0;
  REP(c, W + 1) { ret = max(ret, DP[N][c]); }

  cout << ret << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>

using namespace std;

#define REP(var, count) for (int var = 0; var < count; var++)

int DP[110][10010]; // N???????????§????????????????¨?w

int main(void) {

  int N, W;
  cin >> N >> W;

  REP(i, N) {
    int v, w;
    cin >> v >> w;

    REP(c, W + 1) {
      if (c < w) {
        // ????????????????????????
        DP[i + 1][c] = DP[i][c];
      } else {
        DP[i + 1][c] = max({DP[i][c - w] + v, DP[i + 1][c - w] + v, DP[i][c]});
      }
    }
  }

  int ret = 0;
  REP(c, W + 1) { ret = max(ret, DP[N][c]); }

  cout << ret << endl;
}
Problem p02316 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(n, a)                                                               \
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

int N, W;
int dp[1100] = {};
int v[1000], w[1000];

int main() {
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }

  int ans = 0;

  rep(i, W + 1) {
    rep(j, N) {
      if (i - w[j] < 0)
        continue;
      dp[i] = max(dp[i], dp[i - w[j]] + v[j]);
      ans = max(ans, dp[i]);
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(n, a)                                                               \
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

int N, W;
int dp[110000] = {};
int v[1000], w[1000];

int main() {
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }

  int ans = 0;

  rep(i, W + 1) {
    rep(j, N) {
      if (i - w[j] < 0)
        continue;
      dp[i] = max(dp[i], dp[i - w[j]] + v[j]);
      ans = max(ans, dp[i]);
    }
  }

  cout << ans << endl;
}
Problem p02317 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int b[10001];

int main() {

  int n, temp;
  vector<int> a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a.push_back(temp);
  }
  fill(b, b + n, 1 << 30);
  for (int i = 0; i < n; i++) {
    *lower_bound(b, b + n, a[i]) = a[i];
  }
  int res = 0;
  res = lower_bound(b, b + n, 1 << 30) - b;

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int b[100001];

int main() {

  int n, temp;
  vector<int> a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> temp;
    a.push_back(temp);
  }
  fill(b, b + n, 1 << 30);
  for (int i = 0; i < n; i++) {
    *lower_bound(b, b + n, a[i]) = a[i];
  }
  int res = 0;
  res = lower_bound(b, b + n, 1 << 30) - b;

  cout << res << endl;

  return 0;
}
Problem p02317 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 100010
int n, dat[2 * (MAX_N)-1];

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

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

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

int main() {
  cin >> n;
  int tn = n;
  vector<int> a(100010), b;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b.push_back(a[i]);
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  }
  init(n);
  for (int i = 0; i < tn; i++) {
    int t = query(0, a[i]);
    if (dat[a[i] + n - 1] >= t + 1)
      continue;
    update(a[i], t + 1);
  }
  cout << query(0, tn) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 1000100
int n, dat[2 * (MAX_N)-1];

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

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

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

int main() {
  cin >> n;
  int tn = n;
  vector<int> a(100010), b;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b.push_back(a[i]);
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  }
  init(n);
  for (int i = 0; i < tn; i++) {
    int t = query(0, a[i]);
    if (dat[a[i] + n - 1] >= t + 1)
      continue;
    update(a[i], t + 1);
  }
  cout << query(0, tn) << endl;
}
Problem p02317 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30, Array = 0x1e6;
int main() {
  int n;
  int seq[Array], dp[Array];
  scanf("%d", &n);
  fill(dp, dp + n, INF);
  for (int i = 0; i < n; i++)
    scanf("%d", &seq[i]);
  for (int i = 0; i < n; i++)
    *lower_bound(dp, dp + n, seq[i]) = seq[i];
  cout << lower_bound(dp, dp + n, INF) - dp << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30, Array = int(1e5);
int main() {
  int n;
  int seq[Array], dp[Array];
  scanf("%d", &n);
  fill(dp, dp + n, INF);
  for (int i = 0; i < n; i++)
    scanf("%d", &seq[i]);
  for (int i = 0; i < n; i++)
    *lower_bound(dp, dp + n, seq[i]) = seq[i];
  cout << lower_bound(dp, dp + n, INF) - dp << endl;
  return 0;
}
Problem p02317 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long i64;

int CeilIndex(const vector<i64> &tail, int l, int r, i64 key) {
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (tail[m] >= key)
      r = m;
    else
      l = m;
  }
  return r;
}

vector<i64> LIS(const vector<i64> &vec) {
  vector<i64> tail;
  tail.push_back(vec[0]);

  for (int i = 1; i < vec.size(); ++i) {
    if (vec[i] < tail[0])
      tail[0] = vec[i];
    else if (vec[i] > tail.back())
      tail.push_back(vec[i]);
    else {
      tail[CeilIndex(tail, 0, vec.size(), vec[i])] = vec[i];
    }
  }
  return tail;
}

#include <iostream>

int main() {
  int n;
  cin >> n;
  vector<i64> a;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a.push_back(t);
  }

  cout << LIS(a).size() << endl;
}


Fixed submission:
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long i64;

int CeilIndex(const vector<i64> &tail, int l, int r, i64 key) {
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (tail[m] >= key)
      r = m;
    else
      l = m;
  }
  return r;
}

vector<i64> LIS(const vector<i64> &vec) {
  vector<i64> tail;
  tail.push_back(vec[0]);

  for (int i = 1; i < vec.size(); ++i) {
    if (vec[i] < tail[0])
      tail[0] = vec[i];
    else if (vec[i] > tail.back())
      tail.push_back(vec[i]);
    else {
      tail[CeilIndex(tail, -1, tail.size() - 1, vec[i])] = vec[i];
    }
  }
  return tail;
}

#include <iostream>

int main() {
  int n;
  cin >> n;
  vector<i64> a;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a.push_back(t);
  }

  cout << LIS(a).size() << endl;
}
Problem p02317 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1, -1);
  int l = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (v[l] < a) {
      v[l + 1] = a;
      l++;
    } else {
      *lower_bound(v.begin(), v.end(), a) = a;
    }
  }
  cout << l << endl;

  return 0;
}

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1, -1);
  int l = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (v[l] < a) {
      v[l + 1] = a;
      l++;
    } else {
      *lower_bound(v.begin(), v.begin() + l, a) = a;
    }
  }
  cout << l << endl;

  return 0;
}
Problem p02317 (C++)

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

int main() {
  long long int N;
  cin >> N;
  vector<long long int> dp(N + 1, LLONG_MAX / 6);
  dp[0] = LLONG_MIN;
  for (size_t i = 0; i < N; i++) {
    long long int a;
    cin >> a;
    for (size_t j = 0; j < N; j++) {
      if (dp[j] < a) {
        dp[j + 1] = min(dp[j + 1], a);
      } else {
        break;
      }
    }
  }
  long long int ans = 0;
  for (size_t i = 0; i < N + 1; i++) {
    if (dp[i] != LLONG_MAX / 6) {
      ans = i;
    } else {
      break;
    }
  }
  cout << ans << endl;
}

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

int main() {
  long long int N;
  cin >> N;
  vector<long long int> dp(N + 1, LLONG_MAX / 6);
  dp[0] = LLONG_MIN;
  for (size_t i = 0; i < N; i++) {
    long long int a;
    cin >> a;
    for (size_t j = lower_bound(dp.begin(), dp.end(), a) - dp.begin() - 1;
         j < N; j++) {
      if (dp[j] < a) {
        dp[j + 1] = min(dp[j + 1], a);
      } else {
        break;
      }
    }
  }
  long long int ans = 0;
  for (size_t i = 0; i < N + 1; i++) {
    if (dp[i] != LLONG_MAX / 6) {
      ans = i;
    } else {
      break;
    }
  }
  cout << ans << endl;
}
Problem p02317 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MAX 100000
#define INF 1000000001

int a[MAX];
int l[MAX];
int main() {
  int n;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    l[i] = INF;
  }
  int length = 0;
  l[length] = a[0];
  length++;
  FOR(i, 1, n) {
    if (l[length - 1] < a[i]) {
      l[length] = a[i];
      length++;
    } else {
      int j = length;
      while (1) {
        j--;
        if (l[j] > a[i]) {
          l[j] = a[i];
          break;
        }
      }
    }
  }
  cout << length << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MAX 100000
#define INF 1000000001

int a[MAX];
int l[MAX];
int main() {
  int n;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    l[i] = INF;
  }
  int length = 0;
  l[length] = a[0];
  length++;
  FOR(i, 1, n) {
    if (l[length - 1] < a[i]) {
      l[length] = a[i];
      length++;
    } else {
      REP(j, length) {
        if (j == 0 && l[0] > a[i]) {
          l[0] = a[i];
          break;
        } else if (l[j] < a[i] && l[j + 1] > a[i]) {
          l[j + 1] = a[i];
          break;
        }
      }
    }
  }
  cout << length << endl;

  return 0;
}
Problem p02317 (C++)

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

#define INF 1e9

int main(void) {
  int n, input;
  cin >> n;

  vector<int> dp(n + 1, INF);
  dp[0] = -INF;

  for (int i = 0; i < n; i++) {
    cin >> input;
    vector<int>::iterator it = upper_bound(dp.begin(), dp.end(), input);
    if (*(it - 1) < input)
      *it = input;
  }

  int ans = 0;
  while (dp[ans + 1] < INF) {
    ++ans;
  }
  cout << ans << endl;

  return 0;
}


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

#define INF 1e9

int main(void) {
  int n, input;
  cin >> n;

  vector<int> dp(n + 2, INF);
  dp[0] = -INF;

  for (int i = 0; i < n; i++) {
    cin >> input;
    vector<int>::iterator it = upper_bound(dp.begin(), dp.end(), input);
    if (*(it - 1) < input)
      *it = input;
  }

  int ans = 0;
  while (dp[ans + 1] < INF) {
    ++ans;
  }
  cout << ans << endl;

  return 0;
}
Problem p02317 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int N, a[100000];
int dp[100000];

int main() {
  scanf("%d", &N);
  rep(i, N) scanf("%d", &a[i]);
  int ans = 0;
  rep(i, N) {
    dp[i] = 1;
    FOR(j, 0, i) if (a[j] < a[i]) { dp[i] = max(dp[i], dp[j] + 1); }
    ans = max(ans, dp[i]);
  }
  printf("%d\n", ans);
  return 0;
}

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

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int N, a[100000];
int dp[100000];

int main() {
  scanf("%d", &N);
  rep(i, N) scanf("%d", &a[i]);
  fill(dp, dp + N, INF);
  rep(i, N) { *lower_bound(dp, dp + N, a[i]) = a[i]; }
  printf("%d\n", lower_bound(dp, dp + N, INF) - dp);
  return 0;
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
int L[1000][1000];
int main() {
  string a, b;
  cin >> a;
  cin >> b;

  for (int i = 0; i <= a.size(); ++i)
    L[i][0] = i;
  for (int i = 0; i <= b.size(); ++i)
    L[0][i] = i;

  for (int i = 1; i <= a.size(); ++i) {
    for (int j = 1; j <= b.size(); ++j) {
      int k = 0;
      if (a[i - 1] == b[j - 1])
        k = 0;
      else
        k = 1;
      L[i][j] = min(L[i - 1][j] + 1, min(L[i][j - 1] + 1, L[i - 1][j - 1] + k));
    }
  }
  cout << L[a.size()][b.size()] << endl;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int L[1001][1001];
int main() {
  string a, b;
  cin >> a;
  cin >> b;

  for (int i = 0; i <= a.size(); ++i)
    L[i][0] = i;
  for (int i = 0; i <= b.size(); ++i)
    L[0][i] = i;

  for (int i = 1; i <= a.size(); ++i) {
    for (int j = 1; j <= b.size(); ++j) {
      int k = 0;
      if (a[i - 1] == b[j - 1])
        k = 0;
      else
        k = 1;
      L[i][j] = min(L[i - 1][j] + 1, min(L[i][j - 1] + 1, L[i - 1][j - 1] + k));
    }
  }
  cout << L[a.size()][b.size()] << endl;
}