Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

void OutputError(std::string s) {
  cerr << "\033[93m" << s << "\033[m" << endl;
  return;
}

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  int64_t n, strength;
  cin >> n >> strength;
  int64_t weight1, value1;
  std::vector<int64_t> w, v;
  cin >> weight1 >> value1;
  w.push_back(0);
  v.push_back(value1);
  for (int i = 1; i < n; i++) {
    int m1, m2;
    cin >> m1 >> m2;
    w.push_back(m1 - weight1);
    v.push_back(m2);
  }
  int64_t dp[301][101][100] = {};

  for (int64_t i = 0; i <= n * 3; i++) {
    for (int64_t j = 1; j <= n; j++) {
      for (int64_t k = j - 1; k < n; k++) {
        if (i == 0 && j == 1 && k == 0) {
          dp[0][1][0] = v[0];
          // cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
        } else {
          int64_t not_added = (k - 1 >= 0) ? dp[i][j][k - 1] : 0;

          int64_t added = (i - w[k] >= 0 && j - 1 >= 0 && k - 1 >= 0)
                              ? dp[i - w[k]][j - 1][k - 1] + v[k]
                              : 0;
          dp[i][j][k] = std::max(not_added, added);
          // cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
        }
      }
    }
  }
  int64_t result = 0;
  // cout << "==============" << endl;
  for (int64_t amount = 1; amount <= n; amount++) {
    for (int64_t margin = 0; amount * weight1 + margin <= strength; margin++) {
      int64_t temp = dp[margin][amount][n - 1];
      // cout << margin << " " << amount << " " << temp << endl;
      result = std::max(result, temp);
    }
  }
  cout << result << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

void OutputError(std::string s) {
  cerr << "\033[93m" << s << "\033[m" << endl;
  return;
}

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  int64_t n, strength;
  cin >> n >> strength;
  int64_t weight1, value1;
  std::vector<int64_t> w, v;
  cin >> weight1 >> value1;
  w.push_back(0);
  v.push_back(value1);
  for (int i = 1; i < n; i++) {
    int m1, m2;
    cin >> m1 >> m2;
    w.push_back(m1 - weight1);
    v.push_back(m2);
  }
  int64_t dp[301][101][100] = {};

  for (int64_t i = 0; i <= n * 3; i++) {
    for (int64_t j = 1; j <= n; j++) {
      for (int64_t k = j - 1; k < n; k++) {
        if (i == 0 && j == 1 && k == 0) {
          dp[0][1][0] = v[0];
          // cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
        } else {
          int64_t not_added = (k - 1 >= 0) ? dp[i][j][k - 1] : 0;

          int64_t added = (i - w[k] >= 0 && j - 1 >= 0 && k - 1 >= 0)
                              ? dp[i - w[k]][j - 1][k - 1] + v[k]
                              : 0;
          dp[i][j][k] = std::max(not_added, added);
          // cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
        }
      }
    }
  }
  int64_t result = 0;
  // cout << "==============" << endl;
  for (int64_t amount = 1; amount <= n; amount++) {
    for (int64_t margin = 0;
         margin <= n * 3 && amount * weight1 + margin <= strength; margin++) {
      int64_t temp = dp[margin][amount][n - 1];
      // cout << margin << " " << amount << " " << temp << endl;
      result = std::max(result, temp);
    }
  }
  cout << result << endl;

  return 0;
}