Coin Combinations 1 - CSES

I actually got AC for this problem where it’s clearly mentioned that all coins are distinct integers. But it got hacked. The hack had non-distinct coins.
My code:

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
int main()
    int n, x;
    cin >> n >> x;
    vector <long long> v(n), dp(1000001);
    for (int i = 0; i < n; ++i) cin >> v[i], dp[v[i]] = 1;
    sort(v.begin(), v.end());
    for (int i = 1; i <= x; ++i)
        for (int j = 0; j < n; ++j)
            if (v[j] > i) break;
            else dp[i] += (dp[i-v[j]]), dp[i] %= mod;
    cout << dp[x] << '\n';

Please let me know if my solution is right for distinct coins (it got accepted for the other tests). I just want a yes/no answer (please, no code).

Yes. if you want AC, remove the sort :slight_smile:

I removed the sort and I got WA in 5 more tests :sweat_smile:

you also need to change the break to a continue if you remove the sort.

1 Like

That test is removed now.

1 Like

Yes, it is. It wasn’t valid right? Was it a bug?

There was a bug in the testcase validator.

1 Like

Thanks a lot for answering!

1 Like

//in name of THE GOD
#include <string.h>
#define ll long long int
#define pb push_back
#define tinput int t; cin>>t; while(t–)
#define rep(i,n) for((i)=0; (i)<(n); (i)++)
ll min(ll x, ll y) {
return (x<y)?x:y;

int max(int x, int y) {
return (x>y)?x:y;

using namespace std;
ll dp[1000005],n,mod=1e9+7;
ll coins[1000005];
ll f(ll money)
if(money == 0) return 1;
else if(money<0) return 0;

if(dp[money]!= 0) return dp[money];

for(ll i=0;i<n;i++)
   dp[money] += f(money-coins[i])%mod; 
return dp[money]%mod;


int main()

ll money; cin>>n>>money;
for(ll i=0;i<n;i++) cin>>coins[i];

memset(dp, 0, sizeof(dp));

return 0;

i got TLE for this code

Hey @iaish apply bottom up dp approach