#include<iostream>
#include<cmath>
using namespace std;
void haha(int arr[], int elements, int sum);
int target,answer=0;
int main(){
int elements;
cout << "Enter the number of elements: ";
cin >> elements;
cout << "Enter the target sum: ";
cin >> target;
cout << "Enter the elements of the array: "<<endl;
int arr[elements];
for(int i=0; i<elements; i++) cin >> arr[i];
haha(arr,elements,0);
cout << "Closest sum to "<<target<<" without exceeding it is: "<<answer;
return 0;
}
void haha(int arr[], int elements, int sum){
if(elements<1){
if(sum > answer && sum <= target) {
answer = sum;
}
}
else{
haha( arr, elements-1, sum);
haha( arr, elements-1, sum+arr[elements-1]);
}
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGNtYXRoPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgp2b2lkIGhhaGEoaW50IGFycltdLCBpbnQgZWxlbWVudHMsIGludCBzdW0pOwoKaW50IHRhcmdldCxhbnN3ZXI9MDsKCmludCBtYWluKCl7CiAgaW50IGVsZW1lbnRzOwogIGNvdXQgPDwgIkVudGVyIHRoZSBudW1iZXIgb2YgZWxlbWVudHM6ICI7CiAgY2luID4+IGVsZW1lbnRzOwogIGNvdXQgPDwgIkVudGVyIHRoZSB0YXJnZXQgc3VtOiAiOwogIGNpbiA+PiB0YXJnZXQ7CiAgY291dCA8PCAiRW50ZXIgdGhlIGVsZW1lbnRzIG9mIHRoZSBhcnJheTogIjw8ZW5kbDsKICBpbnQgYXJyW2VsZW1lbnRzXTsKICBmb3IoaW50IGk9MDsgaTxlbGVtZW50czsgaSsrKSBjaW4gPj4gYXJyW2ldOwogIGhhaGEoYXJyLGVsZW1lbnRzLDApOwogIGNvdXQgPDwgIkNsb3Nlc3Qgc3VtIHRvICI8PHRhcmdldDw8IiB3aXRob3V0IGV4Y2VlZGluZyBpdCBpczogIjw8YW5zd2VyOwogIAogIHJldHVybiAwOwp9CgoKdm9pZCBoYWhhKGludCBhcnJbXSwgaW50IGVsZW1lbnRzLCBpbnQgc3VtKXsKCWlmKGVsZW1lbnRzPDEpewoJCWlmKHN1bSA+IGFuc3dlciAmJiBzdW0gPD0gdGFyZ2V0KSB7CgkJCWFuc3dlciA9IHN1bTsKCQl9Cgl9CgllbHNlewoJCWhhaGEoIGFyciwgIGVsZW1lbnRzLTEsICBzdW0pOwoJCWhhaGEoIGFyciwgIGVsZW1lbnRzLTEsICBzdW0rYXJyW2VsZW1lbnRzLTFdKTsKCX0KICAgIAogICAKICB9Cg==