Problem p01450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int main() {
  int n, limit, w[222];
  cin >> n >> limit;
  rep(i, n) cin >> w[i];
  sort(w, w + n, greater<int>());
  w[n] = 0;

  const int mod = 1000000007;
  static int dp[222][12345];
  dp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= limit; ++j) {
      dp[i + 1][j] = dp[i][j];
      if (j >= w[i])
        (dp[i + 1][j] += dp[i][j - w[i]]) %= mod;
    }
  }

  int res = 0;
  int s = 0;
  for (int i = n; i > 0; --i) {
    s += w[i];
    for (int j = limit - w[i - 1] - s + 1; j <= limit - w[i] - s; ++j)
      res += dp[i][j];
  }

  printf("%d\n", res);
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int main() {
  int n, limit, w[222];
  cin >> n >> limit;
  rep(i, n) cin >> w[i];
  sort(w, w + n, greater<int>());
  w[n] = 0;

  const int mod = 1000000007;
  static int dp[222][12345];
  dp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= limit; ++j) {
      dp[i + 1][j] = dp[i][j];
      if (j >= w[i])
        (dp[i + 1][j] += dp[i][j - w[i]]) %= mod;
    }
  }

  int res = 0;
  int use = 0;
  for (int i = n - 1; i >= 0 && use < limit; --i) {
    for (int j = limit - w[i] - use + 1; j <= limit - w[i + 1] - use; ++j)
      (res += dp[i + 1][j]) %= mod;
    use += w[i];
  }

  printf("%d\n", res);
}