what is going wrong here ???
Minimum money needed
You have to add new line I.e., endl after printing your answer for every test case. So add endl at line 43 after printing your answer and do let me know if it passes all test cases or not.
there are no test cases in this…also it is not showing correct o/p for given example as well
5 5
1 2 3 4 5
ans for this is 5
and code is showing 0
Yes I am debugging your code be patient.
Here’s your debugged code
Issue was with what if n==0
What if w< n or val[n-1] == -1
Lemme see, I’ll let you know. Till then just be patient.
hey @hg11110000 try submit this code and let me know if it gets accepted or not
// C++ program to find minimum cost to get exactly
// W Kg with given packets
#include<bits/stdc++.h>
#define INF 1000000
using namespace std;
// cost[] initial cost array including unavailable packet
// W capacity of bag
int MinimumCost(int cost[], int n, int W)
{
// val[] and wt[] arrays
// val[] array to store cost of 'i' kg packet of orange
// wt[] array weight of packet of orange
vector<int> val, wt;
// traverse the original cost[] array and skip
// unavailable packets and make val[] and wt[]
// array. size variable tells the available number
// of distinct weighted packets
int size = 0;
for (int i=0; i<W; i++)
{
if (cost[i]!= -1)
{
val.push_back(cost[i]);
wt.push_back(i+1);
size++;
}
}
//cout<<size<<endl;
n = size;
int min_cost[n+1][W+1];
// fill 0th row with infinity
for (int i=0; i<=n; i++) for(int j=1;j<=W;j++) min_cost[i][j]=INF;
// fill 0'th column with 0
for (int i=1; i<=n; i++)
min_cost[i][0] = 0;
// now check for each weight one by one and fill the
// matrix according to the condition
for (int i=1; i<=n; i++)
{
for (int j=1; j<=W; j++)
{
// wt[i-1]>j means capacity of bag is
// less then weight of item
// cout<<wt[i-1]<<endl;
if (wt[i-1] > j)
min_cost[i][j] = min_cost[i-1][j];
// here we check we get minimum cost either
// by including it or excluding it
else
min_cost[i][j] = min(min_cost[i][j],min(min_cost[i-1][j], min_cost[i][j-wt[i-1]] + val[i-1]));
}
}
// exactly weight W can not be made by given weights
return (min_cost[n][W]==INF)? -1: min_cost[n][W];
}
// Driver program to run the test case
int main()
{
int n,W;
cin>>n>>W;
int val[1005];
for(int i=0;i<W;i++)
{
cin>>val[i];
}
// int n = sizeof(cost)/sizeof(cost[0]);
cout << MinimumCost(val, n, W)<<endl;
return 0;
}
its not about getting accepted or not
its about whats the mistake in my code…it seems correct…why not working
@mr.encoder
check your inbox, i have replied there.