Why Coin Combinations II problem in CSES geting TLE by recursive solution? but iterative solution in accepted.
My recursive code is here:
#include<bits/stdc++.h>
using namespace std;
// #define int long long
int n, m, p, mod=1e9+7, ans=0; vector<vector<int>>dp(110, vector<int>(1e6+9, -1));
vector<int>v;
int ok(int p, int i){
// cerr<<p<<'\n';
if(p==0) return 1;
if(p<0 || i==n) return 0;
if(dp[i][p]!=-1) return dp[i][p];
int k=0;
k=(k+ok(p-v[i], i)); if(k>mod) k-=mod;
k=(k+ok(p, i+1)); if(k>mod) k-=mod;
return dp[i][p]=k;
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m; v.resize(n); for(int i=0; i<n; i++) cin>>v[i]; //sort(v.begin(), v.end());
cout<<ok(m, 0)%mod<<'\n';
}



