Problem p03164 (C++)

Buggy submission (status: Runtime Error):
// Author : ZERO_IQ
// Practice is the only shortcut to improve

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define debug(x, y)                                                            \
  cerr << (#x) << " " << (#y) << " is " << (x) << " " << (y) << endl
#define debug2(x, y, z)                                                        \
  cerr << (#x) << " " << (#y) << " " << (#z) << " is " << (x) << " " << (y)    \
       << " " << (z) << endl
#define endl '\n'

ll const N = 1e7 + 1;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> input;
  long long summ = 0;
  for (int i = 0; i < n; ++i) {
    long long q, qq;
    cin >> q >> qq;
    input.push_back({q, qq});
    summ += qq;
  }
  vector<vector<long long>> dp(n, vector<long long>(summ + 2, 1e18));
  for (int var = 0; var < n; ++var) {
    dp[var][0] = 0;
  }
  ll maxx = 0;
  for (int i = 1; i <= summ; ++i) {
    for (int j = 0; j < n; ++j) {
      if (!j) {
        if (i == input[j].second)
          dp[j][i] = input[j].first;
      } else if (input[j].second > i) {
        dp[j][i] = dp[j - 1][i];
      } else {
        dp[j][i] =
            min(dp[j - 1][i], dp[j - 1][i - input[j].second] + input[j].first);
      }
    }
  }
  for (int i = 0; i <= summ; i++) {
    if (dp[n - 1][i] <= m) {
      maxx = i;
    }
  }
  cout << maxx << endl;
  return 0;
}

Fixed submission:
// Author : ZERO_IQ
// Practice is the only shortcut to improve

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define debug(x, y)                                                            \
  cerr << (#x) << " " << (#y) << " is " << (x) << " " << (y) << endl
#define debug2(x, y, z)                                                        \
  cerr << (#x) << " " << (#y) << " " << (#z) << " is " << (x) << " " << (y)    \
       << " " << (z) << endl
#define endl '\n'

ll const N = 1e7 + 1;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /*#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
  #endif*/
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> input;
  long long summ = 0;
  for (int i = 0; i < n; ++i) {
    long long q, qq;
    cin >> q >> qq;
    input.push_back({q, qq});
    summ += qq;
  }
  vector<vector<long long>> dp(n, vector<long long>(summ + 2, 1e18));
  for (int var = 0; var < n; ++var) {
    dp[var][0] = 0;
  }
  ll maxx = 0;
  for (int i = 1; i <= summ; ++i) {
    for (int j = 0; j < n; ++j) {
      if (!j) {
        if (i == input[j].second)
          dp[j][i] = input[j].first;
      } else if (input[j].second > i) {
        dp[j][i] = dp[j - 1][i];
      } else {
        dp[j][i] =
            min(dp[j - 1][i], dp[j - 1][i - input[j].second] + input[j].first);
      }
    }
  }
  for (int i = 0; i <= summ; i++) {
    if (dp[n - 1][i] <= m) {
      maxx = i;
    }
  }
  cout << maxx << endl;
  return 0;
}