Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. let n = +gets();
  2. let rightSum = 0;
  3. let leftSum = 0;
  4. let seq = gets()
  5. .split(" ")
  6. .map(Number)
  7. .sort((a, b) => a - b);
  8. let maxLeft = 0;
  9. //"271 313 347 419 431 521"
  10. //"149 179 193 229 271 311"
  11. //"24 26 29 31 49 61"
  12. let sum = seq.reduce((accumulator, currentValue) => accumulator + currentValue);
  13. function* subsets(array, offset = 0) {
  14. while (offset < array.length) {
  15. let first = array[offset++];
  16.  
  17. for (let subset of subsets(array, offset)) {
  18. subset.push(first);
  19.  
  20. if (
  21. subset.reduce(
  22. (accumulator, currentValue) => accumulator + currentValue
  23. ) <=
  24. sum / 2
  25. ) {
  26. yield subset;
  27. } else if (offset > 13) {
  28. break;
  29. }
  30. }
  31. }
  32. yield [];
  33. }
  34. //print(maxLeft);
  35. for (let subset of subsets(seq)) {
  36. if (
  37. subset != 0 &&
  38. subset.reduce((accumulator, currentValue) => accumulator + currentValue) >
  39. maxLeft
  40. ) {
  41. maxLeft = subset.reduce(
  42. (accumulator, currentValue) => accumulator + currentValue
  43. );
  44. }
  45. if (
  46. subset != 0 &&
  47. subset.reduce((accumulator, currentValue) => accumulator + currentValue) <
  48. sum / 1100
  49. ) {
  50. break;
  51. }
  52. if (Math.abs(maxLeft - sum / 2) * 2 <= 2) {
  53. break;
  54. }
  55. }
  56.  
  57. print(Math.abs(maxLeft - sum / 2) * 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement