Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define ll long long

#define in(x) cin >> x
#define out(x) cout << x << endl
#define out2(x, y) cout << x << " " << y << endl;

#define MOD 1000000007
#define PI acos(-1)
#define pb push_back

#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl

#define pllll pair<ll, ll>
#define plls pair<ll, string>
#define psll pair<string, ll>

#define For(i, n) for (int i = 0; i < n; i++)

#define phoge out("hoge")
#define phugo out("hugo")

#define vll vector<ll>
#define v2ll vec2<ll>
#define v3ll vec3<ll>

template <typename T> using vec = vector<T>;

template <typename T> using vec2 = vector<vector<T>>;

template <typename T> using vec3 = vector<vector<vector<ll>>>;

template <typename T> using uset = unordered_set<T>;

template <typename T, typename S> using umap = unordered_map<T, S>;

bool inRange(ll a, ll x, ll b) {
  if (a > b) {
    swap(a, b);
  }
  return (a <= x && x <= b);
}

bool between(ll a, ll x, ll b) {
  if (a > b) {
    swap(a, b);
  }
  return (a < x && x < b);
}

template <typename T> void sort(T &a, T &b) {
  if (a > b) {
    swap(a, b);
  }
}

template <typename T> void input(T *data, int n) {
  For(i, n) { cin >> data[i]; }
}

template <typename T, typename S> void input(T *data1, S *data2, int n) {
  For(i, n) { cin >> data1[i] >> data2[i]; }
}

template <typename T> void input(vector<T> &vec, int n) {
  T temp;
  For(i, n) {
    cin >> temp;
    vec.push_back(temp);
  }
}

template <typename T, typename S>
void input(vector<T> &vec1, vector<S> &vec2, int n) {
  T temp1;
  S temp2;
  For(i, n) {
    cin >> temp1 >> temp2;
    vec1.push_back(temp1);
    vec2.push_back(temp2);
  }
}

template <typename T> void sort(vector<T> &ary) {
  sort(ary.begin(), ary.end());
}

template <typename T> void rsort(vector<T> &ary) {
  sort(ary.rbegin(), ary.rend());
}

void rstring(string &str) {
  string temp = str;

  For(i, (int)str.size()) { str[i] = temp[(str.size() - 1) - i]; }
}

template <typename T> void print(T *x, int n) {
  For(i, n) { cout << x[i] << " "; }
  cout << endl;
}

template <typename T> void print(T &x) {
  for (auto t : x) {
    cout << t << " ";
  }
  cout << endl;
}

template <typename T, typename S> void print(pair<T, S> &p) {
  out2(p.first, p.second);
}

template <typename T> T gcd(T n, T m) {
  if (n % m == 0) {
    return m;
  } else {
    return gcd<T>(m, n % m);
  }
}

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  UnionFind() {}

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int size() {
    int count = 0;
    for (int i = 0; i < (int)par.size(); i++) {
      if (par[i] == i) {
        count++;
      }
    }
    return count;
  }
};

template <typename T, typename S> struct _CountMap {
  T data;

  void add(S key, int n) {
    auto itr = data.find(key);
    if (itr == data.end()) {
      data.insert(pair<S, int>(key, n));
    } else {
      itr->second += n;
    }
  }

  auto top() { return data.begin(); }

  int val(S key) { return data[key]; }

  int size() { return data.size(); }

  int sub(S key, int n) {
    auto elm = data.find(key);
    int ret = min(elm->second, n);
    if (ret == elm->second) {
      data.erase(elm);
    }
    elm->second -= ret;
    return ret;
  }

  int &operator[](S key) {
    auto itr = data.find(key);
    if (itr == data.end()) {
      data.insert(pair<S, int>(key, 0));
    }
    return itr->second;
  }
};

struct Area {
  vector<ll> data;

  Area(ll range) : data(range + 1) {}

  void add(pair<ll, ll> newdata) {
    data[newdata.first]++;
    data[newdata.second]--;
  }

  ll num() {
    ll sum = 0;
    ll ret = 0;
    For(i, (int)data.size()) {
      sum += data[i];
      if (sum != 0) {
        ret++;
        for (i++; i < (int)data.size() && (sum += data[i]) != 0; i++)
          ;
      }
    }
    return ret;
  }
};

struct Interval {
  vector<pllll> data;

  void add(ll s, ll f) { data.push_back(pllll(f, s)); }

  ll num() {
    sort(data);

    ll ret = 0;
    ll lastf = -1;
    for (auto t : data) {
      if (t.first == lastf) {
        continue;
      }
      if (t.second >= lastf) {
        lastf = t.first;
        ret++;
      }
    }

    return ret;
  }
};

template <typename T> using CountMap = _CountMap<map<T, int>, T>;

template <typename T>
using CountMapr = _CountMap<multimap<T, int, greater<T>>, T>;

void factoring(ll n, CountMap<ll> &p) {
  for (ll i = 2; i + i <= n; i++) {
    while (n % i == 0) {
      p.add(i, 1);
      n /= i;
    }
  }
}

template <typename T, typename S> using rmap = multimap<T, S, greater<T>>;

template <typename T> using rset = multiset<T, greater<T>>;

vector<ll> fac(2001);  // n!(mod M)
vector<ll> ifac(2001); // k!^{M-2} (mod M)
// a,bの範囲的にこれだけ配列を用意していけば十分

ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % 1000000007;
    x = x * x % 1000000007;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) { // aCbをmod計算
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % 1000000007;
  return tmp * fac[a] % 1000000007;
}

int main() {

  ll n, m, k, a, b, c, x, y, z, h, w, sum = 0;
  string s;

  cin >> n >> w;
  vll W(n + 1), V(n + 1);
  For(i, n) { cin >> W[i + 1] >> V[i + 1]; }

  v2ll dp(n + 1, vll(100001, 1e15));
  dp[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    For(j, 100001) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      dp[i][j + V[i]] = min(dp[i][j + V[i]], dp[i - 1][j] + W[i]);
    }
  }

  ll ans = 0;
  For(i, 100001) {
    if (dp[n][i] <= w) {
      ans = i;
    }
  }

  out(ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define ll long long

#define in(x) cin >> x
#define out(x) cout << x << endl
#define out2(x, y) cout << x << " " << y << endl;

#define MOD 1000000007
#define PI acos(-1)
#define pb push_back

#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl

#define pllll pair<ll, ll>
#define plls pair<ll, string>
#define psll pair<string, ll>

#define For(i, n) for (int i = 0; i < n; i++)

#define phoge out("hoge")
#define phugo out("hugo")

#define vll vector<ll>
#define v2ll vec2<ll>
#define v3ll vec3<ll>

template <typename T> using vec = vector<T>;

template <typename T> using vec2 = vector<vector<T>>;

template <typename T> using vec3 = vector<vector<vector<ll>>>;

template <typename T> using uset = unordered_set<T>;

template <typename T, typename S> using umap = unordered_map<T, S>;

bool inRange(ll a, ll x, ll b) {
  if (a > b) {
    swap(a, b);
  }
  return (a <= x && x <= b);
}

bool between(ll a, ll x, ll b) {
  if (a > b) {
    swap(a, b);
  }
  return (a < x && x < b);
}

template <typename T> void sort(T &a, T &b) {
  if (a > b) {
    swap(a, b);
  }
}

template <typename T> void input(T *data, int n) {
  For(i, n) { cin >> data[i]; }
}

template <typename T, typename S> void input(T *data1, S *data2, int n) {
  For(i, n) { cin >> data1[i] >> data2[i]; }
}

template <typename T> void input(vector<T> &vec, int n) {
  T temp;
  For(i, n) {
    cin >> temp;
    vec.push_back(temp);
  }
}

template <typename T, typename S>
void input(vector<T> &vec1, vector<S> &vec2, int n) {
  T temp1;
  S temp2;
  For(i, n) {
    cin >> temp1 >> temp2;
    vec1.push_back(temp1);
    vec2.push_back(temp2);
  }
}

template <typename T> void sort(vector<T> &ary) {
  sort(ary.begin(), ary.end());
}

template <typename T> void rsort(vector<T> &ary) {
  sort(ary.rbegin(), ary.rend());
}

void rstring(string &str) {
  string temp = str;

  For(i, (int)str.size()) { str[i] = temp[(str.size() - 1) - i]; }
}

template <typename T> void print(T *x, int n) {
  For(i, n) { cout << x[i] << " "; }
  cout << endl;
}

template <typename T> void print(T &x) {
  for (auto t : x) {
    cout << t << " ";
  }
  cout << endl;
}

template <typename T, typename S> void print(pair<T, S> &p) {
  out2(p.first, p.second);
}

template <typename T> T gcd(T n, T m) {
  if (n % m == 0) {
    return m;
  } else {
    return gcd<T>(m, n % m);
  }
}

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  UnionFind() {}

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int size() {
    int count = 0;
    for (int i = 0; i < (int)par.size(); i++) {
      if (par[i] == i) {
        count++;
      }
    }
    return count;
  }
};

template <typename T, typename S> struct _CountMap {
  T data;

  void add(S key, int n) {
    auto itr = data.find(key);
    if (itr == data.end()) {
      data.insert(pair<S, int>(key, n));
    } else {
      itr->second += n;
    }
  }

  auto top() { return data.begin(); }

  int val(S key) { return data[key]; }

  int size() { return data.size(); }

  int sub(S key, int n) {
    auto elm = data.find(key);
    int ret = min(elm->second, n);
    if (ret == elm->second) {
      data.erase(elm);
    }
    elm->second -= ret;
    return ret;
  }

  int &operator[](S key) {
    auto itr = data.find(key);
    if (itr == data.end()) {
      data.insert(pair<S, int>(key, 0));
    }
    return itr->second;
  }
};

struct Area {
  vector<ll> data;

  Area(ll range) : data(range + 1) {}

  void add(pair<ll, ll> newdata) {
    data[newdata.first]++;
    data[newdata.second]--;
  }

  ll num() {
    ll sum = 0;
    ll ret = 0;
    For(i, (int)data.size()) {
      sum += data[i];
      if (sum != 0) {
        ret++;
        for (i++; i < (int)data.size() && (sum += data[i]) != 0; i++)
          ;
      }
    }
    return ret;
  }
};

struct Interval {
  vector<pllll> data;

  void add(ll s, ll f) { data.push_back(pllll(f, s)); }

  ll num() {
    sort(data);

    ll ret = 0;
    ll lastf = -1;
    for (auto t : data) {
      if (t.first == lastf) {
        continue;
      }
      if (t.second >= lastf) {
        lastf = t.first;
        ret++;
      }
    }

    return ret;
  }
};

template <typename T> using CountMap = _CountMap<map<T, int>, T>;

template <typename T>
using CountMapr = _CountMap<multimap<T, int, greater<T>>, T>;

void factoring(ll n, CountMap<ll> &p) {
  for (ll i = 2; i + i <= n; i++) {
    while (n % i == 0) {
      p.add(i, 1);
      n /= i;
    }
  }
}

template <typename T, typename S> using rmap = multimap<T, S, greater<T>>;

template <typename T> using rset = multiset<T, greater<T>>;

vector<ll> fac(2001);  // n!(mod M)
vector<ll> ifac(2001); // k!^{M-2} (mod M)
// a,bの範囲的にこれだけ配列を用意していけば十分

ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % 1000000007;
    x = x * x % 1000000007;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) { // aCbをmod計算
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % 1000000007;
  return tmp * fac[a] % 1000000007;
}

int main() {

  ll n, m, k, a, b, c, x, y, z, h, w, sum = 0;
  string s;

  cin >> n >> w;
  vll W(n + 1), V(n + 1);
  For(i, n) { cin >> W[i + 1] >> V[i + 1]; }

  v2ll dp(n + 1, vll(100001, 1e15));
  dp[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    For(j, 100001) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j + V[i] <= 100001)
        dp[i][j + V[i]] = min(dp[i][j + V[i]], dp[i - 1][j] + W[i]);
    }
  }

  ll ans = 0;
  For(i, 100001) {
    if (dp[n][i] <= w) {
      ans = i;
    }
  }

  out(ans);

  return 0;
}