Problem p01719 (C++)

Buggy submission (status: Runtime Error):
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int dp[11][100010];
int solve(int N, VI &d, VI &p, int X) {
  fill((int *)dp, (int *)dp + 11 * 100010, -1);
  dp[0][0] = 0;
  REP(i, N) {
    REP(x, X + 1) {
      if (dp[i][x] < 0)
        continue;
      dp[i + 1][x] = max(dp[i + 1][x], dp[i][x]);

      for (int tx = x, j = 0; tx <= X; tx += p[i], ++j) {
        dp[i + 1][tx] = max(dp[i + 1][tx], dp[i][x] + d[i] * j);
      }
    }
  }

  return dp[N][X];
}

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

  int N, D, X;
  cin >> N >> D >> X;
  VVI p(D, VI(N));
  REP(d, D) REP(i, N) cin >> p[d][i];
  REP(d, D) p[d].PB(1);

  REP(d, D - 1) { X = solve(N + 1, p[d + 1], p[d], X); }
  cout << X << endl;

  return 0;
}

Fixed submission:
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int dp[12][100010];
int solve(int N, VI &d, VI &p, int X) {
  fill((int *)dp, (int *)dp + 11 * 100010, -1);
  dp[0][0] = 0;
  REP(i, N) {
    REP(x, X + 1) {
      if (dp[i][x] < 0)
        continue;
      dp[i + 1][x] = max(dp[i + 1][x], dp[i][x]);

      for (int tx = x, j = 0; tx <= X; tx += p[i], ++j) {
        dp[i + 1][tx] = max(dp[i + 1][tx], dp[i][x] + d[i] * j);
      }
    }
  }

  return dp[N][X];
}

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

  int N, D, X;
  cin >> N >> D >> X;
  VVI p(D, VI(N));
  REP(d, D) REP(i, N) cin >> p[d][i];
  REP(d, D) p[d].PB(1);

  REP(d, D - 1) { X = solve(N + 1, p[d + 1], p[d], X); }
  cout << X << endl;

  return 0;
}