-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxCutSegments_GFG.cpp
More file actions
72 lines (55 loc) · 1.08 KB
/
MaxCutSegments_GFG.cpp
File metadata and controls
72 lines (55 loc) · 1.08 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Variation of Unbounded Knapsack problem
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int maxCutSegments(int a[],int l,int n)
{
int dp[l+1][n+1];
int i,j;
memset(dp,0,sizeof(dp));
for(i=0;i<4;i++)
{
for(j=0;j<n+1;j++)
{
if(i==0)
dp[i][j] = INT_MIN;
}
}
for(i=1;i<l+1;i++)
{
for(j=1;j<n+1;j++)
{
if(a[i-1]>j)
dp[i][j] = dp[i-1][j];
if(a[i-1]<=j)
dp[i][j] = max(1+dp[i][j-a[i-1]],dp[i-1][j]);
}
}
return dp[l][n];
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[3],l = 0;
set<int> s;
for(int i=0;i<3;i++)
{
int x;
cin>>x;
s.insert(x);
}
for(auto i = s.begin();i != s.end();i++)
{
//inserting only unique numbers
a[l] = *i;
l++;
}
cout<<maxCutSegments(a,l,n)<<"\n";
}
return 0;
}