-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_170unbound.java
More file actions
27 lines (27 loc) · 841 Bytes
/
_170unbound.java
File metadata and controls
27 lines (27 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class _170unbound{
public static int unbounded(int val[],int wt[],int W){
int n = val.length;
int dp[][]=new int[n+1][W+1];
for(int i=1;i<n+1;i++){
for(int j=1;j<W+1;j++){
int v=val[i-1];
int w=wt[i-1];
if(w<=j){
int incProfit = v +dp[i][j-w];
int excProfit = dp[i-1][j];
dp[i][j]=Math.max(incProfit,excProfit);
}else{
int excProfit = dp[i-1][j];
dp[i][j]=excProfit;
}
}
}
return dp[n][W];
}
public static void main(String[] args) {
int val[] = {15,14,10,45,30};
int wt[]={2,5,1,3,4};
int W = 7;
System.out.println(unbounded(val, wt, W));
}
}