Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let n = +gets();
- let rightSum = 0;
- let leftSum = 0;
- let seq = gets()
- .split(" ")
- .map(Number)
- .sort((a, b) => a - b);
- let maxLeft = 0;
- //"271 313 347 419 431 521"
- //"149 179 193 229 271 311"
- //"24 26 29 31 49 61"
- let sum = seq.reduce((accumulator, currentValue) => accumulator + currentValue);
- function* subsets(array, offset = 0) {
- while (offset < array.length) {
- let first = array[offset++];
- for (let subset of subsets(array, offset)) {
- subset.push(first);
- if (
- subset.reduce(
- (accumulator, currentValue) => accumulator + currentValue
- ) <=
- sum / 2
- ) {
- yield subset;
- } else if (offset > 13) {
- break;
- }
- }
- }
- yield [];
- }
- //print(maxLeft);
- for (let subset of subsets(seq)) {
- if (
- subset != 0 &&
- subset.reduce((accumulator, currentValue) => accumulator + currentValue) >
- maxLeft
- ) {
- maxLeft = subset.reduce(
- (accumulator, currentValue) => accumulator + currentValue
- );
- }
- if (
- subset != 0 &&
- subset.reduce((accumulator, currentValue) => accumulator + currentValue) <
- sum / 1100
- ) {
- break;
- }
- if (Math.abs(maxLeft - sum / 2) * 2 <= 2) {
- break;
- }
- }
- print(Math.abs(maxLeft - sum / 2) * 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement