import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; /** * https://www.hackerrank.com/contests/world-codesprint-12/challenges/breaking-sticks */ public class BreakingSticks { private static final int MAX = 1000000; public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); //Scanner scanner = new Scanner(new File("breakingSticks")); int n = scanner.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextLong(); } BreakingSticks breakingSticks = new BreakingSticks(); System.out.println(breakingSticks.longestSequence(a)); } List primes; public BreakingSticks() { primes = primes(MAX); } private List primes(int n) { int iMax = (int) Math.sqrt(Integer.MAX_VALUE); List p = new ArrayList<>(); boolean[] composite = new boolean[n]; for (int i = 2; i < n; i++) { if (! composite[i]) { p.add(i); } if (i <= iMax) { for (int j = i*i; j < n; j += i) { composite[j] = true; } } } return p; } public long longestSequence(long[] a) { return Arrays.stream(a) .map(this::stickSequence) .sum(); } private long stickSequence(long n) { long sum = 1; while (n > 1) { long p = smallestPrimeFactor(n); sum += n; n /= p; } return sum; } private long smallestPrimeFactor(long n) { for (int p: primes) { if (n % p == 0) { return p; } } return n; } }