Problem p01450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#ifndef LIB_REPEAT_HPP
#define LIB_REPEAT_HPP
#define repeat(i, n) for (int i = 0; i < (n); ++i)
#define repeat_from(i, m, n) for (int i = (m); i < (n); ++i)
#define repeat_one(i, n) for (int i = 1; i <= (n); ++i)
#define repeat_rev(i, n) for (int i = (n)-1; 0 <= i; --i)
#define foreach(it, cont)                                                      \\
  for (decltype(cont.begin()) it = cont.begin(); it != cont.end(); ++it)
#endif
#ifndef LIB_TYPEDEF_LL_HPP
#define LIB_TYPEDEF_LL_HPP
typedef long long ll;
typedef unsigned long long ull;
#endif
using namespace std;
const ll mod = 1000000007L;
int main() {
  ios_base::sync_with_stdio(false);
  int N;
  int W;
  cin >> N >> W;
  vector<int> fr(N);
  repeat(i, N) cin >> fr[i];
  sort(fr.begin(), fr.end());
  ll result = 0;
  int acc = 0;
  vector<int> dp[2];
  repeat(ign, N + 1) {
    vector<int> &cur = dp[0];
    vector<int> &prv = dp[1];
    const int lft = W - acc - fr[ign] + 1;
    const int rgt = W - acc + 1;
    if (lft <= 0)
      break;
    // cerr << "acc " << acc << " ignore " << fr[ign] << " left " << lft << "
    // right " << rgt << endl;
    prv.clear();
    prv.resize(lft);
    prv[0] = 1;
    repeat_from(i, ign + 1, N) {
      // cerr << "fr[i] " << fr[i] << endl;
      cur.clear();
      cur.resize(lft);
      if (rgt <= fr[i])
        break;
      repeat(w, lft) if (prv[w]) {
        cur[w] += prv[w];
        if (w + fr[i] < lft)
          cur[w + fr[i]] += prv[w];
        if (lft <= w + fr[i] and w + fr[i] < rgt)
          result += prv[w];
        // cerr << "w+fr[i] " << w+fr[i] << endl;
      }
      // repeat (i,W-acc-fr[ign]+1) cerr << cur[i]; cerr << endl;
      swap(cur, prv);
    }
    result %= mod;
    if (ign < N)
      acc += fr[ign];
  }
  cout << (result + 1) % mod << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#ifndef LIB_REPEAT_HPP
#define LIB_REPEAT_HPP
#define repeat(i, n) for (int i = 0; i < (n); ++i)
#define repeat_from(i, m, n) for (int i = (m); i < (n); ++i)
#define repeat_one(i, n) for (int i = 1; i <= (n); ++i)
#define repeat_rev(i, n) for (int i = (n)-1; 0 <= i; --i)
#define foreach(it, cont)                                                      \\
  for (decltype(cont.begin()) it = cont.begin(); it != cont.end(); ++it)
#endif
#ifndef LIB_TYPEDEF_LL_HPP
#define LIB_TYPEDEF_LL_HPP
typedef long long ll;
typedef unsigned long long ull;
#endif
#ifndef LIB_DEBUG_HPP
#define LIB_DEBUG_HPP
#ifdef DEBUG
#include <cassert>
#define debug(a) a
#else
#define debug(a)
#endif
#endif
using namespace std;
#define PRIME (1000000007L)
vector<int> friends;
vector<int> rest;
ll solve(int i, int w) {
  if (w < 0)
    return 0;
  if (not(i < friends.size()))
    return 1;
  if (rest[i] <= w)
    return 1;
  return (solve(i + 1, w) + solve(i + 1, w - friends[i])) % PRIME;
}
int main() {
  ios_base::sync_with_stdio(false);
  int N;
  int W;
  cin >> N >> W;
  friends.resize(N);
  repeat(i, N) cin >> friends[i];
  sort(friends.rbegin(), friends.rend());
  rest.resize(N);
  rest[N - 1] = friends[N - 1];
  repeat_rev(i, N - 1) rest[i] = rest[i + 1] + friends[i];
  cout << solve(0, W) << endl;
  return 0;
}