Problem p03172 (C++)

Buggy submission (status: Runtime Error):
/*Knapsack problem
Input:
N W
w1 v1
w2 v2
.
.
.
wn vn

Output: print ma possible sum of the values if time that knapsack of capacity W
can contain.
*/

#include <bits/stdc++.h>
using namespace std;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const long long MOD = 1e9 + 7;
using ll = long long;

void add_self(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(int &a, int b) {
  a -= b;
  if (a <= 0) {
    a += MOD;
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> dp(K + 1); // 0-K candies   dp[i] = count when i candies used
  dp[0] = 1;
  for (int child = 0; child < N; ++child) {
    int up_to;
    cin >> up_to;
    vector<int> fake(K + 1);
    for (int used = K; used >= 0; --used) {
      int l = used + 1;
      int r = used + min(up_to, K - used);
      if (l <= r) {
        add_self(fake[l], dp[used]);
        sub_self(fake[r + 1], dp[used]);
      }
    }
    int prefix_sum = 0;
    for (int i = 0; i <= K; ++i) {
      add_self(prefix_sum, fake[i]);
      add_self(dp[i], prefix_sum);
    }
  }
  printf("%d\n", dp[K]);
}

Fixed submission:
/*Knapsack problem
Input:
N W
w1 v1
w2 v2
.
.
.
wn vn

Output: print ma possible sum of the values if time that knapsack of capacity W
can contain.
*/

#include <bits/stdc++.h>
using namespace std;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const long long MOD = 1e9 + 7;
using ll = long long;

void add_self(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(int &a, int b) {
  a -= b;
  if (a <= 0) {
    a += MOD;
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> dp(K + 1); // 0-K candies   dp[i] = count when i candies used
  dp[0] = 1;
  for (int child = 0; child < N; ++child) {
    int up_to;
    cin >> up_to;
    vector<int> fake(K + 1);
    for (int used = K; used >= 0; --used) {
      int l = used + 1;
      int r = used + min(up_to, K - used);
      if (l <= r) {
        add_self(fake[l], dp[used]);
        if (r + 1 <= K) {
          sub_self(fake[r + 1], dp[used]);
        }
      }
    }
    int prefix_sum = 0;
    for (int i = 0; i <= K; ++i) {
      add_self(prefix_sum, fake[i]);
      add_self(dp[i], prefix_sum);
    }
  }
  printf("%d\n", dp[K]);
}