Problem p01450 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000
#define MOD 1000000007

int main() {
  int n, w;
  cin >> n >> w;
  vector<int> vec(n);
  rep(i, n) cin >> vec[i];
  sort(all(vec));

  if (vec[0] > w) {
    cout << 1 << endl;
    return 0;
  }

  vector<vector<long>> dp(w + 1, vector<long>(n + 1, 0));
  // sum=w, min(unused)=n
  dp[0][0] = 1;
  rep(i, n) {
    rep(j, i) for (int k = w; k - vec[i] >= 0;
                   k--)(dp[k][j] += dp[k - vec[i]][j]) %= MOD;
    for (int k = w; k - vec[i] >= 0; k--)
      (dp[k][i + 1] += dp[k - vec[i]][i]) %= MOD;
  }

  long ans = 0;
  rep(i, n) {
    for (int j = max(1, w - vec[i] + 1); j <= w; j++)
      ans += dp[j][i];
  }
  rep(i, w + 1) ans += dp[i][n];

  cout << ans % MOD << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000
#define MOD 1000000007

int main() {
  int n, w;
  cin >> n >> w;
  vector<int> vec(n);
  rep(i, n) cin >> vec[i];
  sort(all(vec));

  if (vec[0] > w) {
    cout << 1 << endl;
    return 0;
  }

  vector<vector<long>> dp(w + 1, vector<long>(n + 1, 0));
  // sum=w, min(unused)=m
  dp[0][0] = 1;
  rep(i, n) {
    rep(j, i) for (int k = w; k - vec[i] >= 0;
                   k--)(dp[k][j] += dp[k - vec[i]][j]) %= MOD;
    for (int k = w; k - vec[i] >= 0; k--)
      (dp[k][i + 1] += dp[k - vec[i]][i]) %= MOD;
  }

  long ans = 0;
  rep(i, n) {
    for (int j = w - vec[i] + 1; j <= w; j++)
      ans += dp[j][i];
  }
  rep(i, w + 1) ans += dp[i][n];

  cout << ans % MOD << endl;

  return 0;
}