Skip to content

Commit 7ff8f54

Browse files
committed
Added Solution - GfG to GitHub
1 parent 4c0df10 commit 7ff8f54

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//{ Driver Code Starts
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
class Array {
6+
public:
7+
template <class T>
8+
static void input(vector<T> &A, int n) {
9+
for (int i = 0; i < n; i++) {
10+
scanf("%d ", &A[i]);
11+
}
12+
}
13+
14+
template <class T>
15+
static void print(vector<T> &A) {
16+
for (int i = 0; i < A.size(); i++) {
17+
cout << A[i] << " ";
18+
}
19+
cout << endl;
20+
}
21+
};
22+
23+
24+
// } Driver Code Ends
25+
26+
class Solution {
27+
public:
28+
int minimumCost(int n, int w, vector<int> &cost) {
29+
// code here
30+
vector<vector<long long>>dp(n+1,vector<long long>(w+1,0));
31+
for(int i=1;i<=w;i++)dp[0][i]=1e9;
32+
for(int i=1;i<=n;i++){
33+
for(int j=1;j<=w;j++){
34+
if(j>=i && cost[i-1]!=-1){
35+
dp[i][j]=min(cost[i-1]+dp[i][j-i],dp[i-1][j]);
36+
}
37+
else dp[i][j]=dp[i-1][j];
38+
}
39+
}
40+
return dp[n][w]==1e9?-1:dp[n][w];
41+
}
42+
};
43+
44+
45+
//{ Driver Code Starts.
46+
47+
int main() {
48+
int t;
49+
scanf("%d ", &t);
50+
while (t--) {
51+
52+
int n;
53+
scanf("%d", &n);
54+
55+
int w;
56+
scanf("%d", &w);
57+
58+
vector<int> cost(n);
59+
Array::input(cost, n);
60+
61+
Solution obj;
62+
int res = obj.minimumCost(n, w, cost);
63+
64+
cout << res << endl;
65+
}
66+
}
67+
68+
// } Driver Code Ends

0 commit comments

Comments
 (0)