Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;

#define rep(i, x) for (ll i = 0; i < (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)

#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmin(x, y) (x = min(x, y))
#define chmax(x, y) (x = max(x, y))

// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

const ll mod = 1e9 + 7;
const ll IMF = 1LL << 29;
const double PI = 3.14159265358979323846;

const int MAX = 2600000;
int n;
ll dp[MAX];

ll sub(ll k, vector<P> v) {
  memset(dp, 0, sizeof(dp));

  for (P p : v) {
    int cost = p.first;
    int val = p.second;
    if (val == 0)
      continue;
    for (ll i = 0; i < MAX; i++) {
      if (i - cost < 0)
        continue;
      dp[i] = max(dp[i], dp[i - cost] + val);
    }
  }

  ll mx = 0;
  for (ll i = 0; i < k + 1; i++) {
    chmax(mx, dp[i]);
  }
  return mx + k;
}

int main() {

  int ax, bx, cx;
  int ay, by, cy;
  cin >> n >> ax >> bx >> cx >> ay >> by >> cy;

  vector<P> A;
  vector<P> B;
  if (ax < ay) {
    A.push_back(P(ax, ay - ax));
  } else {
    B.push_back(P(ay, ax - ay));
  }

  if (bx < by) {
    A.push_back(P(bx, by - bx));
  } else {
    B.push_back(P(by, bx - by));
  }

  if (cx < cy) {
    A.push_back(P(cx, cy - cx));
  } else {
    B.push_back(P(cy, cx - cy));
  }

  ll sum = n;
  sum = sub(sum, A);
  sum = sub(sum, B);
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;

#define rep(i, x) for (ll i = 0; i < (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)

#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmin(x, y) (x = min(x, y))
#define chmax(x, y) (x = max(x, y))

// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

const ll mod = 1e9 + 7;
const ll IMF = 1LL << 29;
const double PI = 3.14159265358979323846;

const int MAX = 26000000;
int n;
ll dp[MAX];

ll sub(ll k, vector<P> v) {
  memset(dp, 0, sizeof(dp));

  for (P p : v) {
    int cost = p.first;
    int val = p.second;
    if (val == 0)
      continue;
    for (ll i = 0; i < MAX; i++) {
      if (i - cost < 0)
        continue;
      dp[i] = max(dp[i], dp[i - cost] + val);
    }
  }

  ll mx = 0;
  for (ll i = 0; i < k + 1; i++) {
    chmax(mx, dp[i]);
  }
  return mx + k;
}

int main() {

  int ax, bx, cx;
  int ay, by, cy;
  cin >> n >> ax >> bx >> cx >> ay >> by >> cy;

  vector<P> A;
  vector<P> B;
  if (ax < ay) {
    A.push_back(P(ax, ay - ax));
  } else {
    B.push_back(P(ay, ax - ay));
  }

  if (bx < by) {
    A.push_back(P(bx, by - bx));
  } else {
    B.push_back(P(by, bx - by));
  }

  if (cx < cy) {
    A.push_back(P(cx, cy - cx));
  } else {
    B.push_back(P(cy, cx - cy));
  }

  ll sum = n;
  sum = sub(sum, A);
  sum = sub(sum, B);
  cout << sum << endl;
  return 0;
}