prithivMLmods/Coder-Stat huggingface dataset:
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    item_count, capacity = [int(x) for x in input().split()]
    weights, values = [], []
    for _ in range(item_count):
        w, v = [int(x) for x in input().split()]
        weights.append(w)
        values.append(v)
    # dp[i][j]
    # = (i番目までの品物を、重さがj以下になるように選んだときの価値の最大値)
    dp = [[0] * (capacity + 1) for _ in range(item_count)]
    for item_i in range(item_count):
        for weight_i in range(1, capacity + 1):
            if (w := weights[item_i]) <= weight_i:
                dp[item_i][weight_i] = max(
                    dp[item_i - 1][weight_i - w] + values[item_i],
                    dp[item_i - 1][weight_i],
                )
            else:
                dp[item_i][weight_i] = dp[item_i - 1][weight_i]
    print(dp[item_count - 1][capacity])


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    item_count, capacity = [int(x) for x in input().split()]
    weights, values = [], []
    for _ in range(item_count):
        w, v = [int(x) for x in input().split()]
        weights.append(w)
        values.append(v)
    # dp[i][j]
    # = (i番目までの品物を、重さがj以下になるように選んだときの価値の最大値)
    dp = [[0] * (capacity + 1) for _ in range(item_count)]
    for item_i in range(item_count):
        for weight_i in range(1, capacity + 1):
            w = weights[item_i]
            if w <= weight_i:
                dp[item_i][weight_i] = max(
                    dp[item_i - 1][weight_i - w] + values[item_i],
                    dp[item_i - 1][weight_i],
                )
            else:
                dp[item_i][weight_i] = dp[item_i - 1][weight_i]
    print(dp[item_count - 1][capacity])


if __name__ == "__main__":
    main()
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, W = map(int, input().split())
    item = []
    for _ in range(N):
        w, v = map(int, input().split())
        item.append((w, v))

    dp = [0] * (W + 1)
    for w, v in item:
        for i in range(W + 1)[::-1]:
            if i - w >= 0:
                tmp = dp[i - w] + v
                if tmp > dp[i]:
                    dp[i] = tmp

    print(max(dp))


if __name__ == "__main__":
    main()


Fixed submission:
import sys

sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, W = map(int, input().split())
    item = []
    for _ in range(N):
        w, v = map(int, input().split())
        item.append((w, v))

    dp = [0] * (W + 1)
    for w, v in item:
        for i in range(w, W + 1)[::-1]:
            tmp = dp[i - w] + v
            if tmp > dp[i]:
                dp[i] = tmp

    print(max(dp))


if __name__ == "__main__":
    main()
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys


def main():
    input = sys.stdin.readline

    N, W = map(int, input().split())
    dp = [0] * (W + 1)

    WV = (map(int, input().split()) for _ in range(N))

    for w, v in WV:
        for j in range(W, w - 1, -1):
            dp[j] = max(dp[j], dp[j - w] + v)

    print(dp[-1])


main()


Fixed submission:
import sys


def main():
    input = sys.stdin.readline

    N, W = map(int, input().split())
    dp = [0] * (W + 1)

    WV = (map(int, input().split()) for _ in range(N))

    for w, v in WV:
        for j in range(W, w - 1, -1):
            tmp = dp[j - w] + v
            if tmp > dp[j]:
                dp[j] = tmp

    print(dp[-1])


main()
Problem p03163 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(10**8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, W = map(int, readline().split())
things = []
for i in range(N):
    weight, value = map(int, readline().split())
    things.append((weight, value))


def solve():
    dp = [0] * (W + 1)
    w0, v0 = things[0]
    for i in range(w0, W + 1):
        dp[i] = v0
    for i in range(1, N):
        dn = dp.copy()
        weight, value = things[i]
        for j in range(weight, W + 1):
            dn[j] = max(dn[j], dp[j - weight] + value)
        dp = dn
    return dp[W]


if __name__ == "__main__":
    print(solve())


Fixed submission:
import sys

sys.setrecursionlimit(10**8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, W = map(int, readline().split())
things = []
for i in range(N):
    weight, value = map(int, readline().split())
    things.append((weight, value))


def solve():
    dp = [0] * (W + 1)
    w0, v0 = things[0]
    for i in range(w0, W + 1):
        dp[i] = v0
    for i in range(1, N):
        dn = list(dp)
        weight, value = things[i]
        for j in range(weight, W + 1):
            dn[j] = max(dn[j], dp[j - weight] + value)
        dp = dn
    return dp[W]


if __name__ == "__main__":
    print(solve())
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
//
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
//
using ll = long long;
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
inline void yes(bool cond) { cond ? puts("Yes") : puts("No"); }
template <typename Type> inline void chmin(Type &a, Type b) {
  if (a > b)
    a = b;
}
template <typename Type> inline void chmax(Type &a, Type b) {
  if (a < b)
    a = b;
}
template <typename Type> inline void sort(Type &arr) {
  std::sort(arr.begin(), arr.end());
}
template <typename Type> inline Type nth(vector<Type> &arr, size_t pos) {
  std::nth_element(arr.begin(), arr.begin() + pos, arr.end());
  return arr[pos];
}
#define BIGP 1000000007
#define INF_I std::numeric_limits<ll>::max()
#define INF_F std::numeric_limits<float>::infinity()
//
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  //
  ll N, W;
  cin >> N >> W;
  vector<pair<ll, ll>> ps;
  rep(i, N) {
    ll w, v;
    cin >> w >> v;
    ps.push_back({w, v});
  }
  vector<vector<ll>> dp;
  dp.resize(N + 1);
  rep(i, N + 1) { dp[i].resize(10001000, 0); }
  for (ll item = 0; item < N; ++item) {
    auto p = ps[item];
    ll w = std::get<0>(p);
    ll v = std::get<1>(p);
    for (ll wi = 0; wi <= W; ++wi) {
      chmax(dp[item + 1][wi], dp[item][wi]);
      if (wi + w <= W) {
        chmax(dp[item + 1][wi + w], dp[item][wi] + v);
      }
    }
  }
  ll h = dp[N][W];
  cout << h;
  //
  return 0;
}

Fixed submission:
//
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
//
using ll = long long;
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
inline void yes(bool cond) { cond ? puts("Yes") : puts("No"); }
template <typename Type> inline void chmin(Type &a, Type b) {
  if (a > b)
    a = b;
}
template <typename Type> inline void chmax(Type &a, Type b) {
  if (a < b)
    a = b;
}
template <typename Type> inline void sort(Type &arr) {
  std::sort(arr.begin(), arr.end());
}
template <typename Type> inline Type nth(vector<Type> &arr, size_t pos) {
  std::nth_element(arr.begin(), arr.begin() + pos, arr.end());
  return arr[pos];
}
#define BIGP 1000000007
#define INF_I std::numeric_limits<ll>::max()
#define INF_F std::numeric_limits<float>::infinity()
//
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  //
  ll N, W;
  cin >> N >> W;
  vector<pair<ll, ll>> ps;
  rep(i, N) {
    ll w, v;
    cin >> w >> v;
    ps.push_back({w, v});
  }
  vector<vector<ll>> dp;
  dp.resize(N + 1);
  rep(i, N + 1) { dp[i].resize(100100, 0); }
  for (ll item = 0; item < N; ++item) {
    auto p = ps[item];
    ll w = std::get<0>(p);
    ll v = std::get<1>(p);
    for (ll wi = 0; wi <= W; ++wi) {
      chmax(dp[item + 1][wi], dp[item][wi]);
      if (wi + w <= W) {
        chmax(dp[item + 1][wi + w], dp[item][wi] + v);
      }
    }
  }
  ll h = dp[N][W];
  cout << h;
  //
  return 0;
}
Problem p03163 (C++)

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

int main() {
  int n, w;
  cin >> n >> w;
  int v[n], wt[n];
  for (int i = 0; i < n; i++) {
    cin >> wt[i];
    cin >> v[i];
  }
  vector<long long> dp(w + 1, 0);
  for (int i = 0; i < n; i++) {
    for (int j = w - i; j >= 0; j--) {

      dp[j + wt[i]] = max(dp[j + wt[i]], v[i] + dp[j]);
    }
    // for (int i = 0; i < w + 1; i++)
    // {
    //     cout << dp[i] << " ";
    // }
    // cout << endl;
  }
  long long ans = 0;
  for (int i = 0; i < w + 1; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

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

int main() {
  int n, w;
  cin >> n >> w;
  int v[n], wt[n];
  for (int i = 0; i < n; i++) {
    cin >> wt[i];
    cin >> v[i];
  }
  vector<long long> dp(w + 1, 0);
  for (int i = 0; i < n; i++) {
    for (long long j = w - wt[i]; j >= 0; j--) {

      dp[j + wt[i]] = max(dp[j + wt[i]], v[i] + dp[j]);
    }
    // for (int i = 0; i < w + 1; i++)
    // {
    //     cout << dp[i] << " ";
    // }
    // cout << endl;
  }
  long long ans = 0;
  for (int i = 0; i < w + 1; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forr(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define ford(i, n) for (int i = n; i > 0; i--)
#define forin(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define debug(x) cerr << #x << " " << x << "\n";
#define yn                                                                     \
  { puts("YES"); }                                                             \
  else {                                                                       \
    puts("NO");                                                                \
  }
#define maxs(x, y) (x = max(x, y))
#define sz(x) (int)(x).size()
#define mins(x, y) (x = min(x, y))
#define newline cout << "\n"
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef vector<ll> vl;

ll dp[20005] = {};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, W, w, v;
  cin >> n >> W;
  forn(i, n) {
    cin >> w >> v;
    forr(j, W - w + 1) { maxs(dp[j + w], dp[j] + v); }
  }
  cout << dp[W];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forr(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define ford(i, n) for (int i = n; i > 0; i--)
#define forin(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define debug(x) cerr << #x << " " << x << "\n";
#define yn                                                                     \
  { puts("YES"); }                                                             \
  else {                                                                       \
    puts("NO");                                                                \
  }
#define maxs(x, y) (x = max(x, y))
#define sz(x) (int)(x).size()
#define mins(x, y) (x = min(x, y))
#define newline cout << "\n"
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef vector<ll> vl;

ll dp[200005] = {};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, W, w, v;
  cin >> n >> W;
  forn(i, n) {
    cin >> w >> v;
    forr(j, W - w + 1) { maxs(dp[j + w], dp[j] + v); }
  }
  cout << dp[W];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fastIO                                                                 \
  ios::sync_with_stdio(NULL);                                                  \
  cin.tie(NULL);

using namespace std;
const int N = 1E5 + 7;

long long f[100][N];
int n, W;
long long w[N], c[N];

int main() {
  fastIO cin >> n >> W;

  for (int i = 1; i <= n; ++i)
    cin >> w[i] >> c[i];

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= W; ++j)
      if (j - w[i] >= 0)
        f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + c[i]);
      else
        f[i][j] = f[i - 1][j];

  cout << f[n][W];
}

Fixed submission:
#include <bits/stdc++.h>
#define fastIO                                                                 \
  ios::sync_with_stdio(NULL);                                                  \
  cin.tie(NULL);

using namespace std;
const int N = 1E5 + 7;

long long f[107][N];
int n, W;
long long w[N], c[N];

int main() {
  fastIO cin >> n >> W;

  for (int i = 1; i <= n; ++i)
    cin >> w[i] >> c[i];

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= W; ++j)
      if (j - w[i] >= 0)
        f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + c[i]);
      else
        f[i][j] = f[i - 1][j];

  cout << f[n][W];
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define vi vector<float>
#define vl vector<long long>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define ll long long
#define umap unordered_map
const int mod = 1e9 + 7;
using namespace std;

#define xx first
#define yy second

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("nnout.txt", "w", stdout);
#endif

  int n, weight;
  cin >> n >> weight;

  int w[n], v[n];

  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

  ll dp[n + 1][weight + 1];

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

  cout << dp[n][weight] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define vi vector<float>
#define vl vector<long long>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define ll long long
#define umap unordered_map
const int mod = 1e9 + 7;
using namespace std;

#define xx first
#define yy second

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("nnout.txt", "w", stdout);
  // #endif

  int n, weight;
  cin >> n >> weight;

  int w[n], v[n];

  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

  ll dp[n + 1][weight + 1];

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

  cout << dp[n][weight] << endl;

  return 0;
}
Problem p03163 (C++)

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

#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) repi(i, 0, n)
#define all(x) (x).begin(), (x).end()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;

const int MAX_N = 100;
const int MAX_M = 1e5;
ll N, W;
ll WV[MAX_N][2] = {{0}};
ll DP[MAX_N][MAX_M] = {{0}};

inline void input() {
  cin >> N >> W;
  rep(i, N) cin >> WV[i][0] >> WV[i][1];
}

inline void solve() {
  rep(i, N) {
    rep(j, W + 1) {
      if (j - WV[i][0] >= 0)
        chmax(DP[i + 1][j], DP[i][j - WV[i][0]] + WV[i][1]);

      chmax(DP[i + 1][j], DP[i][j]);
    }
  }
}

inline void Main() {
  // code
  input();
  solve();
  cout << DP[N][W] << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(20);
  Main();
  return 0;
}


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

#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) repi(i, 0, n)
#define all(x) (x).begin(), (x).end()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;

const int MAX_N = 100 + 10;
const int MAX_M = 1e5 + 10;
ll N, W;
ll WV[MAX_N][2] = {{0}};
ll DP[MAX_N][MAX_M] = {{0}};

inline void input() {
  cin >> N >> W;
  rep(i, N) cin >> WV[i][0] >> WV[i][1];
}

inline void solve() {
  rep(i, N) {
    rep(j, W + 1) {
      if (j - WV[i][0] >= 0)
        chmax(DP[i + 1][j], DP[i][j - WV[i][0]] + WV[i][1]);

      chmax(DP[i + 1][j], DP[i][j]);
    }
  }
}

inline void Main() {
  // code
  input();
  solve();
  cout << DP[N][W] << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(20);
  Main();
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
const int NMAX = 100;
const int WMAX = 100001;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int N, W;
int w[NMAX], v[NMAX];
long long DP[NMAX][WMAX] = {};

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < W + 1; j++) {
      chmax(DP[i + 1][j], DP[i][j]);
      if (j + w[i] > W)
        continue;
      chmax(DP[i + 1][j + w[i]], DP[i][j] + v[i]);
    }
  }
  /*
  for(int i=0; i<N+1; i++){
      for(int j=0; j<W+1; j++) cout << DP[i][j] << " ";
      cout << endl;
  }*/
  cout << DP[N][W] << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
const int NMAX = 101;
const int WMAX = 100001;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int N, W;
int w[NMAX], v[NMAX];
long long DP[NMAX][WMAX] = {};

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < W + 1; j++) {
      chmax(DP[i + 1][j], DP[i][j]);
      if (j + w[i] > W)
        continue;
      chmax(DP[i + 1][j + w[i]], DP[i][j] + v[i]);
    }
  }
  /*
  for(int i=0; i<N+1; i++){
      for(int j=0; j<W+1; j++) cout << DP[i][j] << " ";
      cout << endl;
  }*/
  cout << DP[N][W] << endl;
}
Problem p03163 (C++)

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

using namespace std;

long long int N, v[50], w[50], W;
long long m[100002][102];

long long int resi(long long int W, long long int N) {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        m[j][i] = 0;
      else {
        if (j < w[i - 1]) {
          m[j][i] = m[j][i - 1];
          // cout << j << " " << i << endl;
        } else {
          m[j][i] = max(m[j - w[i - 1]][i - 1] + v[i - 1], m[j][i - 1]);
        }
      }
    }
  }

  // for(int i = 0; i <= N; i++)
  // {
  //     for(int j = 0; j <= W; j++)
  //     {
  //         cout << m[j][i] << "(" << j << ") " ;
  //     }
  //     cout << endl;
  // }

  // cout << "REZ: "<< m[][];

  return m[W][N];
}

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

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

  long long int x = resi(W, N);

  cout << x;

  return 0;
}

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

using namespace std;

long long int N, v[102], w[102], W;
long long m[100002][102];

long long int resi(long long int W, long long int N) {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        m[j][i] = 0;
      else {
        if (j < w[i - 1]) {
          m[j][i] = m[j][i - 1];
          // cout << j << " " << i << endl;
        } else {
          m[j][i] = max(m[j - w[i - 1]][i - 1] + v[i - 1], m[j][i - 1]);
        }
      }
    }
  }

  // for(int i = 0; i <= N; i++)
  // {
  //     for(int j = 0; j <= W; j++)
  //     {
  //         cout << m[j][i] << "(" << j << ") " ;
  //     }
  //     cout << endl;
  // }

  // cout << "REZ: "<< m[][];

  return m[W][N];
}

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

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

  long long int x = resi(W, N);

  cout << x;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll aw[105], av[105], dp[105][105] = {0}, n;
ll fun(ll i, ll w) {
  if (dp[i][w] != -1)
    return dp[i][w];
  if (i == n + 1)
    return 0;
  ll ans1, ans2;
  ans1 = fun(i + 1, w);
  if (w - aw[i] >= 0)
    ans2 = av[i] + fun(i + 1, w - aw[i]);
  else
    ans2 = 0;
  dp[i][w] = max(ans1, ans2);
  return dp[i][w];
}
int main() {
  ll w, i;
  cin >> n >> w;
  for (i = 0; i < n; i++)
    cin >> aw[i] >> av[i];
  for (i = 0; i <= n; i++)
    for (int j = 0; j <= w; j++)
      dp[i][j] = -1;
  cout << fun(0, w);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll aw[105], av[105], dp[105][100007] = {0}, n;
ll fun(ll i, ll w) {
  if (dp[i][w] != -1)
    return dp[i][w];
  if (i == n + 1)
    return 0;
  ll ans1, ans2;
  ans1 = fun(i + 1, w);
  if (w - aw[i] >= 0)
    ans2 = av[i] + fun(i + 1, w - aw[i]);
  else
    ans2 = 0;
  dp[i][w] = max(ans1, ans2);
  return dp[i][w];
}
int main() {
  ll w, i;
  cin >> n >> w;
  for (i = 0; i < n; i++)
    cin >> aw[i] >> av[i];
  for (i = 0; i <= n; i++)
    for (int j = 0; j <= w; j++)
      dp[i][j] = -1;
  cout << fun(0, w);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 100100;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, W;
  cin >> N >> W;
  // dp[i]:重さiで達成できる最大の価値
  int dp[d5];
  for (int i = 0; i < d5; i++)
    dp[i] = -1;
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    int w, v;
    cin >> w >> v;
    for (int i = W; i >= 0; i--) {
      if (dp[i] == -1)
        continue;
      else {
        dp[i + w] = max(dp[i + w], dp[i] + v);
      }
    }
  }
  int res = 0;
  for (int i = 0; i <= W; i++)
    res = max(res, dp[i]);
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 200100;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, W;
  cin >> N >> W;
  // dp[i]:重さiで達成できる最大の価値
  int dp[d5];
  for (int i = 0; i < d5; i++)
    dp[i] = -1;
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    int w, v;
    cin >> w >> v;
    for (int i = W; i >= 0; i--) {
      if (dp[i] == -1)
        continue;
      else {
        dp[i + w] = max(dp[i + w], dp[i] + v);
      }
    }
  }
  int res = 0;
  for (int i = 0; i <= W; i++)
    res = max(res, dp[i]);
  cout << res << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int N, W;
vector<int> v, w;

ll dp[100][100100];

void input() {
  cin >> N >> W;
  v.resize(N);
  w.resize(N);
  rep(i, N) cin >> w[i] >> v[i];
}

ll solve() {
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= W; j++) {
      if (j - w[i] >= 0) {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  return dp[N][W];
}

int main() {
  input();
  cout << solve() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int N, W;
vector<int> v, w;

ll dp[110][100100];

void input() {
  cin >> N >> W;
  v.resize(N);
  w.resize(N);
  rep(i, N) cin >> w[i] >> v[i];
}

ll solve() {
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= W; j++) {
      if (j - w[i] >= 0) {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  return dp[N][W];
}

int main() {
  input();
  cout << solve() << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define r1 101
#define c1 10001
ll int knapsack(ll int *w, ll int *val, ll int sum, ll int n, ll int dp[][c1]) {
  if (sum <= 0 || n <= 0)
    return 0;

  if (dp[n][sum] != -1)
    return dp[n][sum];
  if (w[n - 1] <= sum)
    return dp[n][sum] =
               max(val[n - 1] + knapsack(w, val, sum - w[n - 1], n - 1, dp),
                   knapsack(w, val, sum, n - 1, dp));
  else
    return dp[n][sum] = knapsack(w, val, sum, n - 1, dp);
}
int main() {
  ll int n, W;
  cin >> n >> W;
  ll int w[n], val[n];
  for (int i = 0; i < n; i++)
    cin >> w[i] >> val[i];

  ll int dp[r1][c1];
  for (int i = 0; i < r1; i++) {
    for (int j = 0; j < c1; j++)
      dp[i][j] = -1;
  }
  cout << knapsack(w, val, W, n, dp);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define r1 101
#define c1 100001
ll int knapsack(ll int *w, ll int *val, ll int sum, ll int n, ll int dp[][c1]) {
  if (sum <= 0 || n <= 0)
    return 0;

  if (dp[n][sum] != -1)
    return dp[n][sum];
  if (w[n - 1] <= sum)
    return dp[n][sum] =
               max(val[n - 1] + knapsack(w, val, sum - w[n - 1], n - 1, dp),
                   knapsack(w, val, sum, n - 1, dp));
  else
    return dp[n][sum] = knapsack(w, val, sum, n - 1, dp);
}
int main() {
  ll int n, W;
  cin >> n >> W;
  ll int w[n], val[n];
  for (int i = 0; i < n; i++)
    cin >> w[i] >> val[i];

  ll int dp[r1][c1];
  for (int i = 0; i < r1; i++) {
    for (int j = 0; j < c1; j++)
      dp[i][j] = -1;
  }
  cout << knapsack(w, val, W, n, dp);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):

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

const int MAXN = 100;
const int MAXW = 100000;

unsigned long long dp[MAXN + 1][MAXW + 1];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, W;
  cin >> n >> W;
  int v[n], w[n];
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i <= W; i++) {
    dp[0][i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (w[i] <= j) {
        dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

  cout << dp[n][W];
  return 0;
}


Fixed submission:

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

const int MAXN = 100;
const int MAXW = 100000;

unsigned long long dp[MAXN + 1][MAXW + 1];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, W;
  cin >> n >> W;
  int v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i <= W; i++) {
    dp[0][i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (w[i] <= j) {
        dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

  cout << dp[n][W];
  return 0;
}
Problem p03163 (C++)

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

#define watch(x) cout << (#x) << " = " << x << endl;
#define mkp make_pair
#define mkt make_tuple
#define pb push_back
#define ll long long
#define ld long double

using namespace std;

int n, W;
ll dp[110][10010];
int w[110], v[110];

int main() {
  scanf("%d%d", &n, &W);

  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &w[i], &v[i]);
  }

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

  dp[1][W] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      if (j >= w[i])
        dp[i + 1][j - w[i]] = max(dp[i + 1][j - w[i]], dp[i][j] + v[i]);
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }

  ll ans = -1;
  for (int i = 0; i <= W; i++)
    ans = max(ans, dp[n + 1][i]);

  printf("%lld\n", ans);

  return 0;
}

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

#define watch(x) cout << (#x) << " = " << x << endl;
#define mkp make_pair
#define mkt make_tuple
#define pb push_back
#define ll long long
#define ld long double

using namespace std;

int n, W;
ll dp[110][100010];
int w[110], v[110];

int main() {
  scanf("%d%d", &n, &W);

  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &w[i], &v[i]);
  }

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

  dp[1][W] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      if (j >= w[i])
        dp[i + 1][j - w[i]] = max(dp[i + 1][j - w[i]], dp[i][j] + v[i]);
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }

  ll ans = -1;
  for (int i = 0; i <= W; i++)
    ans = max(ans, dp[n + 1][i]);

  printf("%lld\n", ans);

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
int wt[105];
int val[105];
int ans[105][10005];
int N, w;
int ks(int n, int c) {
  if (ans[n][c] != -1)
    return ans[n][c];
  if (n == 0 or c == 0)
    return 0;
  if (wt[n - 1] > c)
    return ks(n - 1, c);
  int a = ks(n - 1, c);
  int b = val[n - 1] + ks(n - 1, c - wt[n - 1]);
  ans[n][c] = max(a, b);
  return ans[n][c];
}
void solve() {
  cin >> N >> w;
  for (int i = 0; i < N; ++i) {
    cin >> wt[i] >> val[i];
  }
  memset(ans, -1, sizeof(ans));

  cout << ks(N, w);
}
int32_t main() { solve(); }


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int wt[105];
int val[105];
int ans[105][100005];
int N, w;
int ks(int n, int c) {
  if (ans[n][c] != -1)
    return ans[n][c];
  if (n == 0 or c == 0)
    return 0;
  if (wt[n - 1] > c)
    return ks(n - 1, c);
  int a = ks(n - 1, c);
  int b = val[n - 1] + ks(n - 1, c - wt[n - 1]);
  ans[n][c] = max(a, b);
  return ans[n][c];
}
void solve() {
  cin >> N >> w;
  for (int i = 0; i < N; ++i) {
    cin >> wt[i] >> val[i];
  }
  memset(ans, -1, sizeof(ans));

  cout << ks(N, w);
}
int32_t main() { solve(); }
Problem p03163 (C++)

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

using namespace std;

long long recurse(int n, int w, vector<int> weights, vector<long long> values,
                  vector<vector<long long>> dp) {
  if (w <= 0 || n <= 0)
    return 0;
  else {
    if (dp[w][n] != -1)
      return dp[w][n];
    else {
      if (w - weights[n - 1] >= 0)
        dp[w][n] = max(recurse(n - 1, w - weights[n - 1], weights, values, dp) +
                           values[n - 1],
                       recurse(n - 1, w, weights, values, dp));
      else
        dp[w][n] = recurse(n - 1, w, weights, values, dp);
      return dp[w][n];
    }
  }
}

int main(int argc, char const *argv[]) {
  int n, w;
  long long temp;
  cin >> n >> w;
  vector<int> weights(n, 0);
  vector<long long> values(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> temp;
    weights[i] = temp;
    cin >> temp;
    values[i] = temp;
  }

  vector<vector<long long>> dp(w + 1, vector<long long>(n + 1, -1));

  cout << recurse(n, w, weights, values, dp);

  return 0;
}


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

using namespace std;

long long recurse(int n, int w, vector<int> &weights, vector<long long> &values,
                  vector<vector<long long>> &dp) {
  if (w <= 0 || n <= 0)
    return 0;
  else {
    if (dp[w][n] != -1)
      return dp[w][n];
    else {
      if (w - weights[n - 1] >= 0)
        dp[w][n] = max(recurse(n - 1, w - weights[n - 1], weights, values, dp) +
                           values[n - 1],
                       recurse(n - 1, w, weights, values, dp));
      else
        dp[w][n] = recurse(n - 1, w, weights, values, dp);
      return dp[w][n];
    }
  }
}

int main(int argc, char const *argv[]) {
  int n, w;
  long long temp;
  cin >> n >> w;
  vector<int> weights(n, 0);
  vector<long long> values(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> temp;
    weights[i] = temp;
    cin >> temp;
    values[i] = temp;
  }

  vector<vector<long long>> dp(w + 1, vector<long long>(n + 1, -1));

  cout << recurse(n, w, weights, values, dp);

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF (1 << 30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;

ll dp[110][100100];

int main() {
  ll n, wc;
  cin >> n >> wc;
  vector<ll> w(n, 0);
  vector<ll> v(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> w[i + 1] >> v[i + 1];
  }
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= wc; ++j) {
      dp[i][j] = dp[i - 1][j];
      if (j >= w[i])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[n][wc] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF (1 << 30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;

ll dp[110][100100];

int main() {
  ll n, wc;
  cin >> n >> wc;
  vector<ll> w(n + 10, 0);
  vector<ll> v(n + 10, 0);
  for (int i = 0; i < n; ++i) {
    cin >> w[i + 1] >> v[i + 1];
  }
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= wc; ++j) {
      dp[i][j] = dp[i - 1][j];
      if (j >= w[i])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[n][wc] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> weight(N), value(N);
  rep(i, N) cin >> weight[i] >> value[i];
  ll dp[110][100010];
  rep(i, N + 1) dp[0][i] = 0;
  rep(i, N) {
    rep(j, W + 1) {
      if (W >= weight[i])
        dp[i + 1][j] = max(dp[i][j - weight[i]] + value[i], dp[i][j]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  cout << dp[N][W] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> weight(N), value(N);
  rep(i, N) cin >> weight[i] >> value[i];
  ll dp[110][100010];
  rep(i, N + 1) dp[0][i] = 0;
  rep(i, N) {
    rep(j, W + 1) {
      if (j >= weight[i])
        dp[i + 1][j] = max(dp[i][j - weight[i]] + value[i], dp[i][j]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long w[1010];
long long v[1010];
int N;
long long dp[110][100010];
long long mo(int pos, long long W) {
  if (W < 0)
    return -1e9;
  if (pos == N) {
    return 0;
  }
  long long re = dp[pos][W];
  if (re != -1)
    return re;

  re = max(mo(pos + 1, W), mo(pos + 1, W - w[pos]) + v[pos]);
  return re;
}
int main() {
  int W;
  while (cin >> N >> W) {
    memset(dp, -1, sizeof dp);
    for (int i = 0; i < N; i++) {
      cin >> w[i] >> v[i];
    }
    cout << mo(0, W) << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long w[1010];
long long v[1010];
int N;
long long dp[110][100010];
long long mo(int pos, long long W) {
  if (W < 0)
    return -1e9;
  if (pos == N) {
    return 0;
  }
  long long &re = dp[pos][W];
  if (re != -1)
    return re;

  re = max(mo(pos + 1, W), mo(pos + 1, W - w[pos]) + v[pos]);
  return re;
}
int main() {
  int W;
  while (cin >> N >> W) {
    memset(dp, -1, sizeof dp);
    for (int i = 0; i < N; i++) {
      cin >> w[i] >> v[i];
    }
    cout << mo(0, W) << "\n";
  }
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define ll long long
#define debug(x) cout << "debug: " << x << endl
#define MOD 1e9 + 7
#define INF 1e9

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n, w;
  cin >> n >> w;
  vector<vector<int>> v(n, vector<int>(2));
  rep(i, n) rep(j, 2) cin >> v[i][j];
  vector<vector<ll>> dp(110, vector<ll>(10010, 0));
  rep(i, n) {
    rep(j, w + 1) {
      if (j - v[i][0] >= 0)
        chmax(dp[i + 1][j], dp[i][j - v[i][0]] + v[i][1]);
      chmax(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[n][w] << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define ll long long
#define debug(x) cout << "debug: " << x << endl
#define MOD 1e9 + 7
#define INF 1e9

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n, w;
  cin >> n >> w;
  vector<vector<int>> v(n, vector<int>(2));
  rep(i, n) rep(j, 2) cin >> v[i][j];
  vector<vector<ll>> dp(110, vector<ll>(100100, 0));
  rep(i, n) {
    rep(j, w + 1) {
      if (j - v[i][0] >= 0)
        chmax(dp[i + 1][j], dp[i][j - v[i][0]] + v[i][1]);
      chmax(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[n][w] << endl;
}
Problem p03163 (C++)

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

typedef vector<int> vi;
typedef pair<int, int> pii;
#define endl "\n"
#define sd(val) scanf("%d", &val)
#define ss(val) scanf("%s", &val)
#define sl(val) scanf("%lld", &val)
#define debug(val) printf("check%d\n", val)
#define all(v) v.begin(), v.end()
#define PB push_back
#define MP make_pair
#define FF first
#define SS second
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define clr(val) memset(val, 0, sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ                                                                     \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

ll n, w;
ll we[105];
ll va[105];
ull mem[105][105];

ll dp(ll n, ll w) {
  if (w == 0 || n == 0)
    return 0;
  if (mem[n][w] != -1)
    return mem[n][w];
  if (we[n] <= w)
    return mem[n][w] = max(va[n] + dp(n - 1, w - we[n]), dp(n - 1, w));
  else {
    return mem[n][w] = dp(n - 1, w);
  }
}

int main() {
  cin >> n >> w;

  for (ll i = 1; i <= n; i++) {
    cin >> we[i] >> va[i];
  }

  memset(mem, -1, sizeof(mem));

  cout << dp(n, w) << endl;

  return 0;
}

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

typedef vector<int> vi;
typedef pair<int, int> pii;
#define endl "\n"
#define sd(val) scanf("%d", &val)
#define ss(val) scanf("%s", &val)
#define sl(val) scanf("%lld", &val)
#define debug(val) printf("check%d\n", val)
#define all(v) v.begin(), v.end()
#define PB push_back
#define MP make_pair
#define FF first
#define SS second
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define clr(val) memset(val, 0, sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ                                                                     \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

ll n, w;
ll we[105];
ll va[105];
ll mem[105][100005];

ll dp(ll n, ll w) {
  if (w == 0 || n == 0)
    return 0;
  if (mem[n][w] != -1)
    return mem[n][w];
  if (we[n] <= w)
    return mem[n][w] = max(va[n] + dp(n - 1, w - we[n]), dp(n - 1, w));
  else {
    return mem[n][w] = dp(n - 1, w);
  }
}

int main() {
  cin >> n >> w;

  for (ll i = 1; i <= n; i++) {
    cin >> we[i] >> va[i];
  }

  memset(mem, -1, sizeof(mem));

  cout << dp(n, w) << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  long long int n, w;
  cin >> n >> w;
  long long int weight[n + 1], value[n + 1];
  for (ll int i = 1; i <= n; i++) {
    cin >> weight[i] >> value[i];
  }
  ll int dp[n + 1][w + 1];
  for (ll int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (ll int i = 0; i <= w; i++) {
    dp[0][i] = 0;
  }
  for (ll int i = 1; i <= n; i++) {
    for (ll int j = 1; j <= w; j++) {
      if (j >= weight[i]) {
        ll int cap = j - weight[i], add = 0;
        for (ll int k = 0; k < i; k++) {
          if (dp[k][cap] > add) {
            add = dp[k][cap];
          }
        }
        dp[i][j] = value[i] + add;
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  /*for(int i=0;i<=n;i++){
      for(int j=0;j<=w;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  ll int ans = 0;
  for (ll int i = 0; i <= n; i++) {
    if (dp[i][w] > ans) {
      ans = dp[i][w];
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  long long int n, w;
  cin >> n >> w;
  long long int weight[n + 1], value[n + 1];
  for (ll int i = 1; i <= n; i++) {
    cin >> weight[i] >> value[i];
  }
  ll int dp[n + 1][w + 1];
  for (ll int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (ll int i = 0; i <= w; i++) {
    dp[0][i] = 0;
  }
  for (ll int i = 1; i <= n; i++) {
    for (ll int j = 1; j <= w; j++) {
      if (j >= weight[i]) {
        ll int cap = j - weight[i], add = 0;
        add = dp[i - 1][cap];
        dp[i][j] = max(value[i] + add, dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  /*for(int i=0;i<=n;i++){
      for(int j=0;j<=w;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  ll int ans = 0;
  for (ll int i = 0; i <= n; i++) {
    if (dp[i][w] > ans) {
      ans = dp[i][w];
    }
  }
  cout << ans << endl;
}