-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLabexam.cpp
More file actions
40 lines (39 loc) · 755 Bytes
/
Labexam.cpp
File metadata and controls
40 lines (39 loc) · 755 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
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
using namespace std;
int max1(int x,int y)
{
if(x>y)
return x;
else
return y;
}
int main()
{
int n;
cin>>n;
int va[n],wt[n];
for(int i=0;i<n;i++)
{
cin>>va[i]>>wt[i];
}
int maxW,maxv;
cin>>maxW;
int Knap[n+1][maxW+1];
for(int i=0;i<=n;i++)
{
for(int j=0;j<=maxW;j++)
{
if(i==0||j==0)
Knap[i][j]=0;
else
{
if(wt[i-1]<=j)
Knap[i][j]=max1(Knap[i-1][j],Knap[i-1][j-wt[i-1]]+va[i-1]);
else
Knap[i][j]=Knap[i-1][j];
}
}
}
maxv=Knap[n][maxW];
cout<<"Maximum value :"<<maxv;
}