Problem p03734 (C++)

Buggy submission (status: Runtime Error):

/*{{{*/
// #pragma GCC optimize("O3")

#include <bits/stdc++.h>

#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

typedef long long ll;
typedef std::pair<int, int> pii;

std::string Name = __FILE__;
std::string iput = Name.substr(0, Name.length() - 4) + ".in";
std::string oput = Name.substr(0, Name.length() - 4) + ".out";

template <class T> inline bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
template <class T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}

template <class T> inline T &read(T &x) {
  static int f;
  static char c;
  for (f = 1; !isdigit(c = getchar());) {
    if (c == '-')
      f = -1;
  }
  for (x = 0; isdigit(c); c = getchar()) {
    x = x * 10 + c - 48;
  }
  return x *= f;
}

template <class T> inline void write(T x, const char p = '\n') {
  static int top;
  static int s[30];
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  do
    s[++top] = x % 10 + 48;
  while (x /= 10);
  while (top)
    putchar(s[top--]);
  putchar(p);
}
/*}}}*/

const int maxn = 100 + 5;

long long n;
long long W;
long long w[maxn], ww[maxn], v[maxn];
long long f[maxn][3 * maxn];

long long check(long long K) {
  memset(f, 0, sizeof f);
  long long rem = std::min(W - K * w[1], 3 * K);
  for (int i = 1; i <= n; ++i)
    for (int j = i; j; --j)
      for (int k = rem; k >= ww[i]; --k)
        chkmax(f[j][k], f[j - 1][k - ww[i]] + v[i]);
  long long ret = 0;
  for (int j = 0; j <= rem; ++j)
    chkmax(ret, f[K][j]);
  return ret;
}

void exec() {
  read(n), read(W);
  for (int i = 1; i <= n; ++i) {
    read(w[i]);
    read(v[i]);
    ww[i] = w[i] - w[1];
  }
  int l = 1, r = n, mid1, mid2;
  while (l + 5 < r) {
    mid1 = (l + l + r) / 3;
    mid2 = (l + r + r) / 3;
    if (check(mid1) < check(mid2))
      l = mid1;
    else
      r = mid2;
  }
  long long ans = 0;
  for (int i = l; i <= r; ++i)
    chkmax(ans, check(i));
  write(ans);
}

/*{{{*/
int main() {
  if (fopen(iput.c_str(), "r") != NULL) {
    freopen(iput.c_str(), "r", stdin);
    freopen(oput.c_str(), "w", stdout);
  }

  exec();

  fclose(stdin);
  fclose(stdout);

  return 0;
}
/*}}}*/


Fixed submission:

/*{{{*/
// #pragma GCC optimize("O3")

#include <bits/stdc++.h>

#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

typedef long long ll;
typedef std::pair<int, int> pii;

std::string Name = __FILE__;
std::string iput = Name.substr(0, Name.length() - 4) + ".in";
std::string oput = Name.substr(0, Name.length() - 4) + ".out";

template <class T> inline bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
template <class T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}

template <class T> inline T &read(T &x) {
  static int f;
  static char c;
  for (f = 1; !isdigit(c = getchar());) {
    if (c == '-')
      f = -1;
  }
  for (x = 0; isdigit(c); c = getchar()) {
    x = x * 10 + c - 48;
  }
  return x *= f;
}

template <class T> inline void write(T x, const char p = '\n') {
  static int top;
  static int s[30];
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  do
    s[++top] = x % 10 + 48;
  while (x /= 10);
  while (top)
    putchar(s[top--]);
  putchar(p);
}
/*}}}*/

const int maxn = 100 + 5;

long long n;
long long W;
long long w[maxn], ww[maxn], v[maxn];
long long f[maxn][3 * maxn];

long long check(long long K) {
  memset(f, 0, sizeof f);
  long long rem = std::min(W - K * w[1], 3 * K);
  if (rem < 0)
    return 0;
  for (int i = 1; i <= n; ++i)
    for (int j = i; j; --j)
      for (int k = rem; k >= ww[i]; --k)
        chkmax(f[j][k], f[j - 1][k - ww[i]] + v[i]);
  long long ret = 0;
  for (int j = 0; j <= rem; ++j)
    chkmax(ret, f[K][j]);
  return ret;
}

void exec() {
  read(n), read(W);
  for (int i = 1; i <= n; ++i) {
    read(w[i]);
    read(v[i]);
    ww[i] = w[i] - w[1];
  }
  int l = 1, r = n, mid1, mid2;
  while (l + 5 < r) {
    mid1 = (l + l + r) / 3;
    mid2 = (l + r + r) / 3;
    if (check(mid1) < check(mid2))
      l = mid1;
    else
      r = mid2;
  }
  long long ans = 0;
  for (int i = l; i <= r; ++i)
    chkmax(ans, check(i));
  write(ans);
}

/*{{{*/
int main() {
  if (fopen(iput.c_str(), "r") != NULL) {
    freopen(iput.c_str(), "r", stdin);
    freopen(oput.c_str(), "w", stdout);
  }

  exec();

  fclose(stdin);
  fclose(stdout);

  return 0;
}
/*}}}*/