Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll ARR = 1e5 + 2;
const ll MOD = 1e9 + 7;
const ll INF = MOD;
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define gof for (long long int i = 0; i < n; i++)
#define for1(i, s, e) for (long long int i = s; i < e; ++i)
#define weight first
#define value second
ll power(ll x, ll y) {
  ll res = 1;
  x = x % MOD;
  if (x == 0)
    return 0;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
ll max(ll a[], ll n) {
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    ans = max(ans, a[i]);
  return ans;
}

ll min(ll a[], ll n) {
  ll ans = INF;
  for (ll i = 0; i < n; i++)
    ans = min(ans, a[i]);
  return ans;
}
bool isPrime(ll N) {
  for (int i = 2; i * i <= N; ++i) {
    if (N % i == 0)
      return false;
  }
  return true;
}

void solve() {
  ll n, W;
  cin >> n >> W;
  ll wt[n + 1], val[n + 1];
  for1(i, 0, n) cin >> wt[i] >> val[i];
  ll i, w;
  ll K[n + 1][W + 1];

  for (i = 0; i <= n; i++) {
    for (w = 0; w <= W; w++) {
      if (i == 0 || w == 0)
        K[i][w] = 0;
      else if (wt[i - 1] <= w)
        K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
      else
        K[i][w] = K[i - 1][w];
    }
  }

  cout << K[n][W];

  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("test.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll ARR = 1e5 + 2;
const ll MOD = 1e9 + 7;
const ll INF = MOD;
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define gof for (long long int i = 0; i < n; i++)
#define for1(i, s, e) for (long long int i = s; i < e; ++i)
#define weight first
#define value second
ll power(ll x, ll y) {
  ll res = 1;
  x = x % MOD;
  if (x == 0)
    return 0;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
ll max(ll a[], ll n) {
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    ans = max(ans, a[i]);
  return ans;
}

ll min(ll a[], ll n) {
  ll ans = INF;
  for (ll i = 0; i < n; i++)
    ans = min(ans, a[i]);
  return ans;
}
bool isPrime(ll N) {
  for (int i = 2; i * i <= N; ++i) {
    if (N % i == 0)
      return false;
  }
  return true;
}

void solve() {
  ll n, W;
  cin >> n >> W;
  ll wt[n + 1], val[n + 1];
  for1(i, 0, n) cin >> wt[i] >> val[i];
  ll i, w;
  ll K[n + 1][W + 1];

  for (i = 0; i <= n; i++) {
    for (w = 0; w <= W; w++) {
      if (i == 0 || w == 0)
        K[i][w] = 0;
      else if (wt[i - 1] <= w)
        K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
      else
        K[i][w] = K[i - 1][w];
    }
  }

  cout << K[n][W];

  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}