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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1000000007;

LL dp[210][10010];
LL sum[210];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  vector<LL> ws(N);
  REP(i, N) cin >> ws[i];
  SORT(ws);

  dp[N + 1][0] = dp[N][0] = 1;
  for (int i = N - 1; i >= 0; --i) {
    for (int w = 0; w <= W; ++w) {
      dp[i][w] = dp[i + 1][w];
      if (w - ws[i] >= 0)
        dp[i][w] = (dp[i][w] + dp[i + 1][w - ws[i]]) % MOD;
    }
  }
  sum[0] = 0;
  REP(i, N) sum[i + 1] = sum[i] + ws[i];
  // REP(i,N+1) cout << sum[i] << " ";cout << endl;

  LL ans = 0;
  if (W < ws[0])
    ans = 1;
  else {
    for (int i = 0; i < N; ++i) {
      if (sum[i] > W)
        break;
      for (int w = 0; w <= W - sum[i]; ++w) {
        if (!(W - ws[i] < w + sum[i]))
          continue;
        ans = (ans + dp[i + 1][w]) % MOD;
        //		cout << i << ", " << w << endl;
      }
    }
    if (sum[N] <= W)
      ans = (ans + 1) % MOD;
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1000000007;

LL dp[210][10010];
LL sum[210];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  vector<LL> ws(N);
  REP(i, N) cin >> ws[i];
  SORT(ws);
  ws[N + 1] = MOD;

  dp[N + 1][0] = dp[N][0] = 1;
  for (int i = N - 1; i >= 0; --i) {
    for (int w = 0; w <= W; ++w) {
      dp[i][w] = dp[i + 1][w];
      if (w - ws[i] >= 0)
        dp[i][w] = (dp[i][w] + dp[i + 1][w - ws[i]]) % MOD;
    }
  }
  sum[0] = 0;
  REP(i, N) sum[i + 1] = sum[i] + ws[i];

  LL ans = 0;
  if (W < ws[0])
    ans = 1;
  else {
    for (int i = 0; i < N; ++i) {
      if (sum[i] > W)
        break;
      for (int w = 0; w <= W - sum[i]; ++w) {
        if (!(W - ws[i] < w + sum[i]))
          continue;
        ans = (ans + dp[i + 1][w]) % MOD;
        //		cout << i << ", " << w << endl;
      }
    }
    if (sum[N] <= W)
      ans = (ans + 1) % MOD;
  }
  cout << ans << endl;

  return 0;
}