CSES - Shared codeLink to this code:
https://cses.fi/paste/6abc0a5e697668422edfe7/
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long ll;
ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a % b);}
ll inf = 1000000000;
int dp[10000000];
int mod = 1000000000+7;
int permtation(int sum, vector<int> &coin, int n){
if(sum==0) return 1;
if(sum<0) return 0;
if(dp[sum]!=-1) return dp[sum];
int count = 0;
for(int i=0;i<n;i++){
if(sum-coin[i]<0) break;
count+=(permtation(sum-coin[i],coin,n));
count%=mod;
}
return dp[sum] = count;
}
void solve(){
int sum;
int n;
cin>>n>>sum;
vector<int> coin(n);
for(int i=0;i<n;i++){
cin>>coin[i];
}
sort(coin.begin(),coin.end());
memset(dp,-1,sizeof(dp));
int ans = permtation(sum,coin,n);
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0) ;
cin.tie(0) ; cout.tie(0) ;
cout.precision(20);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T=1; //cin>>T;
while(T--){
solve();
if(T)cout << endl;
}
return 0;
}