// JavaScript program for above approach
function the_allpossible_getter(allsub, v, res, n, index) {
// Recursive function to get all possible subsets of v
if (index >= n) {
// If index is equal to or greater than n,
// append current subset to allsub and return
allsub.push(res.slice());
return;
}
res.push(v[index]);
index += 1;
the_allpossible_getter(allsub, v, res, n, index);
index += 1;
res.pop();
the_allpossible_getter(allsub, v, res, n, index);
}
// Main function
function main() {
const v = [1, 2, -4, -2, 3, 0];
const n = 6;
let res = [];
let allsub = [];
the_allpossible_getter(allsub, v, res, n, 0);
let maxsum = Number.MIN_SAFE_INTEGER;
let maxsub = [];
for (let sub of allsub) {
// Iterate over all subsets to
// find the subset with maximum sum
let s = sub.reduce((a, b) => a + b, 0);
if (s > maxsum) {
maxsum = s;
maxsub = sub;
}
}
console.log(maxsum);
console.log(...maxsub);
}
// Call the main function
main();