Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mt make_tuple
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e8;
const ll INF = 1e16;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  ll n, m;
  cin >> n >> m;
  vp in(n);
  rep(i, n) cin >> in[i].first >> in[i].second;
  ll out = 0, q = in[0].first;
  loop(i, 1, n) in[i].first -= q;
  in[0].first = 0;
  rep(i, n + 1) {
    ll W = m - q * i;
    if (W < 0)
      continue;
    vvi dp(i + 1, vi(400));
    rep(w, n) {
      for (int j = i - 1; j >= 0; j--)
        rep(k, 400) {
          if (k + in[w].first < 400)
            dp[j + 1][k + in[w].first] =
                max(dp[j + 1][k + in[w].first], dp[j][k] + in[w].second);
        }
    }
    //		rep(j,10)cout<<" "<<dp[i][j];cout<<endl;
    rep(j, W + 1) out = max(out, dp[i][j]);
  }
  cout << out << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mt make_tuple
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e8;
const ll INF = 1e16;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  ll n, m;
  cin >> n >> m;
  vp in(n);
  rep(i, n) cin >> in[i].first >> in[i].second;
  ll out = 0, q = in[0].first;
  loop(i, 1, n) in[i].first -= q;
  in[0].first = 0;
  rep(i, n + 1) {
    ll W = m - q * i;
    if (W < 0)
      continue;
    vvi dp(i + 1, vi(400));
    rep(w, n) {
      for (int j = i - 1; j >= 0; j--)
        rep(k, 400) {
          if (k + in[w].first < 400)
            dp[j + 1][k + in[w].first] =
                max(dp[j + 1][k + in[w].first], dp[j][k] + in[w].second);
        }
    }
    //		rep(j,10)cout<<" "<<dp[i][j];cout<<endl;
    rep(j, min(W + 1, 400LL)) out = max(out, dp[i][j]);
  }
  cout << out << endl;
}