Problem p03734 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long
    ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807

template <class T> using vec = std::vector<T>;
template <class T> using vec2 = vec<vec<T>>;
template <class T> using vec3 = vec<vec<vec<T>>>;

#define DEBUG

ll getNum(ll n) { return n / 1000; }

ll getWeight(ll n, ll w1) { return getNum(n) * w1 + n % 1000; }

ll encode(ll num, ll w, ll w1) { return num * 1000 + (w - w1 * num); }

void solve(long long N, long long W, std::vector<long long> w,
           std::vector<long long> v) {
  // value[i][j]は(i-1)番のアイテムまで使って重さjが実現できるかどうか(-1) &
  // そのときの最大価値
  vector<vector<ll>> value(N + 1, vector<ll>(3000000 + 1, -1));
  value[0][0] = 0;
  ll w1 = w.at(0);

  for (int i = 1; i < value.size(); i++) {
    for (int j = 0; j < value.at(0).size(); j++) {
      // vs[i-1], ws[i-1]と、インデックス1つずれにきをつける！
      int taking = -1;
      // とる
      ll num = getNum(j);
      if (getWeight(j, w1) > W) {
        continue;
      }
      ll subWeight = getWeight(j, w1) - w[i - 1];
      if (subWeight >= 0 && encode(num - 1, subWeight, w1) >= 0 &&
          num - 1 >= 0 && value[i - 1][encode(num - 1, subWeight, w1)] >= 0) {
        taking = value[i - 1][encode(num - 1, subWeight, w1)] + v[i - 1];
      }
      int notaking = -1;
      // とらない
      if (value[i - 1][j] >= 0) {
        notaking = value[i - 1][j];
      }
      value[i][j] = max(taking, notaking);
    }
  }

  auto res = *max_element(value[N].begin(), value[N].end());

  cout << res << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long W;
  scanf("%lld", &W);
  std::vector<long long> w(N);
  std::vector<long long> v(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &w[i]);
    scanf("%lld", &v[i]);
  }
  solve(N, W, std::move(w), std::move(v));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long
    ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807

template <class T> using vec = std::vector<T>;
template <class T> using vec2 = vec<vec<T>>;
template <class T> using vec3 = vec<vec<vec<T>>>;

#define DEBUG

ll getNum(ll n) { return n / 1000; }

ll getWeight(ll n, ll w1) { return getNum(n) * w1 + n % 1000; }

ll encode(ll num, ll w, ll w1) { return num * 1000 + (w - w1 * num); }

void solve(long long N, long long W, std::vector<long long> w,
           std::vector<long long> v) {
  // value[i][j]は(i-1)番のアイテムまで使って重さjが実現できるかどうか(-1) &
  // そのときの最大価値
  vector<vector<ll>> value(N + 1, vector<ll>(300000 + 1, -1));
  value[0][0] = 0;
  ll w1 = w.at(0);

  for (int i = 1; i < value.size(); i++) {
    for (int j = 0; j < value.at(0).size(); j++) {
      // vs[i-1], ws[i-1]と、インデックス1つずれにきをつける！
      int taking = -1;
      // とる
      ll num = getNum(j);
      if (getWeight(j, w1) > W) {
        continue;
      }
      ll subWeight = getWeight(j, w1) - w[i - 1];
      if (subWeight >= 0 && encode(num - 1, subWeight, w1) >= 0 &&
          num - 1 >= 0 && value[i - 1][encode(num - 1, subWeight, w1)] >= 0) {
        taking = value[i - 1][encode(num - 1, subWeight, w1)] + v[i - 1];
      }
      int notaking = -1;
      // とらない
      if (value[i - 1][j] >= 0) {
        notaking = value[i - 1][j];
      }
      value[i][j] = max(taking, notaking);
    }
  }

  auto res = *max_element(value[N].begin(), value[N].end());

  cout << res << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long W;
  scanf("%lld", &W);
  std::vector<long long> w(N);
  std::vector<long long> v(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &w[i]);
    scanf("%lld", &v[i]);
  }
  solve(N, W, std::move(w), std::move(v));
  return 0;
}