Solution is not working if i update the variable to INT_MAX.

Revision en1, by Thanos_I_am_inevitable, 2024-01-27 15:46:20
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i, p, q) for (int i = p; i < q; i++)

ll dp[1000003];

long long solve(int n, int sum, int *ar) {
    if (sum == 0) return 0;
    if (dp[sum] != INT_MAX) return dp[sum];

    // long long ans = 10000000;
     long long ans = INT_MAX;

    for (int i = 0; i < n; i++) {
        if (sum - ar[i] >= 0) {
            ans = min(ans, 1 + solve(n, sum - ar[i], ar));
        }
    }

    dp[sum] = ans;
    return ans;
}
int main()
{
    int n,m;
    cin >> n>>m;
    int ar[n];
    f(i,0,n)cin>>ar[i];
    f(i,0,1000003)dp[i]= INT_MAX;
    int a =solve(n,m, ar);
    cout<< ((a== 10000000)? -1 : a);
}
Tags dp, recursion

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English Thanos_I_am_inevitable 2024-01-27 16:20:20 122
en6 English Thanos_I_am_inevitable 2024-01-27 15:52:11 26 Tiny change: ' INT_MAX; but it is' -> ' INT_MAX; and it is taking too long but it is'
en5 English Thanos_I_am_inevitable 2024-01-27 15:51:31 0 (published)
en4 English Thanos_I_am_inevitable 2024-01-27 15:51:10 121
en3 English Thanos_I_am_inevitable 2024-01-27 15:50:31 75
en2 English Thanos_I_am_inevitable 2024-01-27 15:48:28 964
en1 English Thanos_I_am_inevitable 2024-01-27 15:46:20 786 Initial revision (saved to drafts)