Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

struct Card {
  int attack;
  int cost;
};

class Solver {
private:
  vector<Card> cards;
  int card_num;
  int cost_max;

public:
  Solver() {}

  Solver(vector<Card> cards_, int card_num_, int cost_max_)
      : cards(cards_), card_num(card_num_), cost_max(cost_max_) {}

  void exec() {
    vector<vector<int>> dp =
        vector<vector<int>>(card_num + 1, vector<int>(cost_max + 1, 0));
    for (int i = 0; i < card_num; ++i) {
      for (int j = 0; j <= cost_max; ++j) {
        if (i == 0) {
          if (j - cards[i].cost >= 0)
            dp[i][j] = cards[i].attack;
          continue;
        }
        if (j - cards[i].cost >= 0) {
          dp[i][j] =
              max(dp[i - 1][j - cards[i].cost] + cards[i].attack, dp[i - 1][j]);
        } else {
          dp[i][j] = dp[i - 1][j];
        }
        // cout << dp[i][j] << " ";
      }
      // cout << endl;
    }
    cout << dp[card_num - 1][cost_max] << endl;
  }
};

int main(int argc, char *argv[]) {
  // start from 1 to ignore script name; argv[0] will be a name of processing
  // file. for (int i = 1; i < argc; i++) {
  //  printf ("argv[%i]: %s\\n", i, argv[i]);
  // }
  int card_num, cost_max;
  // int arg_int = 1;
  // card_num = atoi(argv[arg_int]);
  // arg_int++;
  // cost_max = atoi(argv[arg_int]);
  // arg_int++;

  vector<Card> cards;
  // for (int i = 0; i < card_num; ++i)
  // {
  //     Card tmp_card = { atoi(argv[arg_int]),  atoi(argv[arg_int + 1])};
  //     arg_int += 2;
  //     cards.push_back(tmp_card);
  // }

  cin >> card_num;
  cin >> cost_max;
  cards.resize(card_num);
  for (int i = 0; i < card_num; ++i) {
    cin >> cards[i].attack >> cards[i].cost;
  }

  Solver s = Solver(cards, card_num, cost_max);
  s.exec();
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  vector::_M_default_append


Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct Card {
  int attack;
  int cost;
};

class Solver {
private:
  vector<Card> cards;
  int card_num;
  int cost_max;

public:
  Solver() {}

  Solver(vector<Card> cards_, int card_num_, int cost_max_)
      : cards(cards_), card_num(card_num_), cost_max(cost_max_) {}

  void exec() {
    vector<vector<int>> dp =
        vector<vector<int>>(card_num + 1, vector<int>(cost_max + 1, 0));
    for (int i = 0; i < card_num; ++i) {
      for (int j = 0; j <= cost_max; ++j) {
        if (i == 0) {
          if (j - cards[i].cost >= 0)
            dp[i][j] = cards[i].attack;
          continue;
        }
        if (j - cards[i].cost >= 0) {
          dp[i][j] =
              max(dp[i - 1][j - cards[i].cost] + cards[i].attack, dp[i - 1][j]);
        } else {
          dp[i][j] = dp[i - 1][j];
        }
        // cout << dp[i][j] << " ";
      }
      // cout << endl;
    }
    cout << dp[card_num - 1][cost_max] << endl;
  }
};

int main(int argc, char *argv[]) {
  // start from 1 to ignore script name; argv[0] will be a name of processing
  // file. for (int i = 1; i < argc; i++) {
  //  printf ("argv[%i]: %s\\n", i, argv[i]);
  // }
  int card_num, cost_max;
  // int arg_int = 1;
  // card_num = atoi(argv[arg_int]);
  // arg_int++;
  // cost_max = atoi(argv[arg_int]);
  // arg_int++;

  vector<Card> cards;
  for (int i = 0; i < card_num; ++i)
    // {
    //     Card tmp_card = { atoi(argv[arg_int]),  atoi(argv[arg_int + 1])};
    //     arg_int += 2;
    //     cards.push_back(tmp_card);
    // }

    cin >> card_num;
  cin >> cost_max;
  cards.resize(card_num);
  for (int i = 0; i < card_num; ++i) {
    cin >> cards[i].attack >> cards[i].cost;
  }

  Solver s = Solver(cards, card_num, cost_max);
  s.exec();
  return 0;
}