#!/bin/python3 import sys ''' Subproblem: OPT[i] = max sequence to finish i-length chocolate Base Cases: OPT[0] = 0, OPT[1] = 1 Recursion: OPT[i] = OPT[j] + 1, where j is the factor of i with max sequence No. of subproblems: length of chocolate(n) Running Time: n * O(n) = O(n^2) Solution: OPT(n) ''' def get_factors(chocolate_length): for i in range(1, chocolate_length//2 + 1): if chocolate_length % i == 0: yield i def longestSequence(a): biggest_chocolate = max(a) OPT = [0 for i in range(biggest_chocolate + 1)] OPT[0] = 0 OPT[1] = 1 for i in range(2, biggest_chocolate + 1): max_seq = 0 for factor in get_factors(i): if max_seq < (i//factor)*OPT[factor]: max_seq = (i//factor)*OPT[factor] OPT[i] = max_seq + 1 res = 0 for chocolate in a: res += OPT[chocolate] return res # Return the length of the longest possible sequence of moves. if __name__ == "__main__": n = int(input().strip()) a = list(map(int, input().strip().split(' '))) result = longestSequence(a) print(result)