a=list(map(int,input().split()))
l=[]
total=0
count=0
for i in sorted(a,reverse=True):
if(i+total>n):
continue
else:
l.append(i)
count+=1
total+=i
if total==n:
break
print(count)
l.sort()
for i in l:
print(a.index(i),end=" ")
it will work if you did removed elements, first time remove the last element second time remove the element before the last elementā¦
the first array will be : 3 3 4 10 ==> 14
the second : 3 3 4 ==>>10
the third : 3 3 10 ==> 16
the forth : 3 4 10 ==> 14
and at the end we will take the best one.
The only issue you will got is with the execution time, and that what happened to me is that it took more than 40second
you can return the same element twice and you canāt return greater than 4 elements.
you did returned among the number : 5, it is impossible to find 5 there cos the possiblities to be returned are 0, 1, 2, 3.
And thatās the main purpose of that problem set, is to find the best closer result, so you should assume that it wonāt be always the same as the max number
v[0].clear();
for (auto i = v[1].begin(); i != v[1].end(); ++i)
v[0].push_back(*i);
}
}
cout<<v[0].size()<<endl;
for (auto ir = v[0].rbegin(); ir != v[0].rend(); ++ir)
cout << *ir << " ";
}
This is my codde
15 4
3 4 6 8
It gives me 2
2 3 max as 14
but i can go with max as 3 +6+8 as 15 but my code was acc how ??