import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static long longestSequence(long[] a, Map movesPerLength) { // Return the length of the longest possible sequence of moves. long numOfMoves = 0; // the length of the longest possible sequence of moves for a is the sum of the lengths // of the longest possible sequences of moves for each stick for (int i = 0; i < a.length; i++) { long currentStick = a[i]; // memoization table, no need to look up the longest sequence of moves if (movesPerLength.containsKey(currentStick)) { numOfMoves += movesPerLength.get(currentStick); } else { long numMaxSubMoves = currentStick + 1; // splitting into 1's // only need to check first half, redundant checking of the latter half for (long j = 2; j <= Math.ceil(Math.sqrt(currentStick)); j++) { // break it into equal sized pieces of size j if (currentStick % j == 0) { long numPieces = currentStick/j; // 1 move + number of moves for the remainder of the pieces * number of pieces long numSubMoves = 1 + numPieces * longestSequence(new long[] {j}, movesPerLength); numMaxSubMoves = numSubMoves > numMaxSubMoves ? numSubMoves : numMaxSubMoves; numSubMoves = 1 + j * longestSequence(new long[] {currentStick/j}, movesPerLength); numMaxSubMoves = numSubMoves > numMaxSubMoves ? numSubMoves : numMaxSubMoves; } } movesPerLength.put(currentStick, numMaxSubMoves); numOfMoves += numMaxSubMoves; } } return numOfMoves; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] a = new long[n]; for(int a_i = 0; a_i < n; a_i++){ a[a_i] = in.nextLong(); } // observation: movesPerLength[prime] = prime + 1 // observation: splitting by a number greater than 1 is always better than splitting into 1 Map movesPerLength = new HashMap<>(); movesPerLength.put(1L, 1L); // stick of length 1 can only be eaten movesPerLength.put(2L, 3L); // stick of length 2 can only be broken into 2, and then each can be eaten for a total of 3 movesPerLength.put(3L, 4L); movesPerLength.put(4L, 7L); long result = longestSequence(a, movesPerLength); System.out.println(result); in.close(); } }