Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int main()
- {
- // dp[x] --> the number of ways such that we have used x candies till this level
- int n, k;
- cin >> n >> k;
- vector<int> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- vector<int> dp(k + 1, 0);
- dp[0] = 1;
- int MOD = 1e9 + 7;
- for(int i = 0; i < n; i++) {
- vector<int> pref_sum(k + 1, 0);
- for(int j = k; j >= 0; j--) {
- int L = j + 1;
- int R = j + min(v[i], k - j);
- if(L <= R) {
- pref_sum[L] += dp[j];
- pref_sum[L] %= MOD;
- if(R + 1 <= k) {
- pref_sum[R + 1] -= dp[j];
- if(pref_sum[R + 1] < 0) {
- pref_sum[R + 1] += MOD;
- }
- }
- }
- }
- int sum = 0;
- for(int c = 0; c <= k; c++) {
- sum += pref_sum[c];
- sum %= MOD;
- dp[c] += sum;
- dp[c] %= MOD;
- }
- }
- cout << dp[k] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement