#!/bin/ruby require 'prime' @cache = {} def longest(val) return @cache[val] if @cache[val] max = Math.sqrt(val) return 1 if val == 1 || max < 2 max_val = 1 Prime.each(max) do |div| next if val%div > 0 m = val/div if @cache[m].nil? @cache[m] = longest(m) end sum = m + @cache[m] max_val = sum if max_val.nil? || max_val < sum break end @cache[val] = max_val return @cache[val] end def longestSequence(a) # Return the length of the longest possible sequence of moves. sum = 0 a.each do |e| longest_e = e == 1 ? 0 : longest(e) sum += longest_e + e end return sum end n = gets.strip.to_i a = gets.strip a = a.split(' ').map(&:to_i) result = longestSequence(a) puts result