process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// // cases // 1x1 = 1 // 1x7 -> 7x1 = 8 // 1x4 -> 2x2 -> 4x1 = 7 // 1x6 -> 3x2 -> 6x1 = 10 // 1x8 -> 2x4 -> 4x2 -> 8x1 = 15 // 1x12 -> 2x6 -> 6x2 -> 12x1 // 1x12 -> 4x3 -> 12x1 // 1x12 -> 3x4 -> 6x2 -> 12x1 // 1x24 -> ??? -> 24x1 = 46 // 1x24 -> 3x8 -> 6x4 -> 12x2 -> 24x1 = 46 // 1x24 -> 3x8 -> 6x4 -> 12x2 (1 + 3 + 6 + 12 = 22) // 1x24 -> 2x12 -> 6x4 -> 12x2 (1 + 2 + 6 + 12 = 21) // 1x24 -> 4x6 -> 12x2 -> 24x1 (1 + 4 + 12 = 17) // 1x24 -> 6x4 -> 12x2 -> 24x1 (1+ 6 + 12 = 18) // 1x24 -> 8x3 -> 24x1 (1 + 8 = 9) // 1x24 -> 12x2 -> 24x1 (1 + 12 = 13) // 8x3? 12x2? function longestSequence(sticks) { // Return the length of the longest possible sequence of moves. // Strategy to test: // 1. if n is 1, return 1 // 2. if n % 2 is 1, return n + 1 // 3. else if (n / 2) % 2 is 0, uh... // Question: how do we choose to divide 24 into 3 pieces? // 24 % 4 = 0 // 24 / 4 = 6 // 24 / 6 = 4 // Idea: something having to do with mod // Idea: something having to do with n_roots // Idea: split into largest value that results in an even number that isn't 2 // Theory: split into largest power of 2 possible? 2, 4, 8, 16, 32 const processStick = (stick) => { if (stick === 1) { return 1; } // Find the largest possible power of 4 stick can be divided and transform stick into pieces of that size let newPieceSize = 1; while (stick % (newPieceSize * 2) === 0) { newPieceSize *= 2; } const numNewPieces = stick / newPieceSize; return numNewPieces + processStick(newPieceSize); } // 4 -> 2 pieces // 6 -> 3 pieces // 8 -> 2 pieces (making 4s) // 12 -> 4 pieces (making 6s) // 24 -> 3 pieces (making 8s) return sticks.reduce((acc, x) => { let breaks = 1; let numDivisions = 1; let stickSize = x; while (stickSize !== 1) { // console.log('enter', breaks, stickSize) let newStickSize = 1; while (stickSize % (newStickSize * 2) === 0 && newStickSize * 2 < stickSize) { newStickSize = newStickSize * 2; } const numNewPieces = stickSize / newStickSize; // console.log('new div', numNewPieces, 'x', newStickSize); breaks += numDivisions * numNewPieces; numDivisions *= numNewPieces; // console.log('exit', breaks, newStickSize); if (stickSize === newStickSize) { throw new Error("this ain't right"); } stickSize = newStickSize; } // console.log('processStick', x, breaks) return acc + breaks; }, 0); } function main() { var n = parseInt(readLine()); a = readLine().split(' '); a = a.map(Number); var result = longestSequence(a); process.stdout.write("" + result + "\n"); }