-
Notifications
You must be signed in to change notification settings - Fork 0
/
A.c
64 lines (59 loc) · 1.04 KB
/
A.c
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
#include<stdio.h>
#include<stdlib.h>
int main()
{
long long a[22];
long long subsetsum[1000000];
int val[22];
int n,k;
int j;
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++)
{
scanf(" %lld",&a[i]);
}
long long min= 10000001;
long long pos=-1;
for(int i=1;i<(1<<n);i++)
{
j=i;
subsetsum[i]=0;
for(int h=0;h<n;h++)
{
if(j%2==1)
subsetsum[i]+=a[n-1-h];
j=j/2;
}
if(subsetsum[i]<=k)
{
if((k-subsetsum[i])<min)
{
min=k-subsetsum[i];
pos=i;
}
}
}
int ct=0;
j=pos;
for(int y=0;y<n;y++)
{
if(j%2==1)
{
val[n-1-y]=1;
ct++;
}
else
{
val[n-1-y]=0;
}
j=j/2;
}
printf("%d ",ct);printf("\n");
for(int y=0;y<n;y++)
{
if(val[y]==1)
printf("%d ",y+1);
}
printf("\n");
return 0;
}