Non-Trivial Common Divisor
InterviewTime limit3sMemory limit512 MB
Pick a subset of the given numbers that all share a common divisor greater than 1, maximizing the subset's sum.
- Level
Medium6 of 10
- Topics
- Number theory, Hash map, Greedy, Math
- Solved
- No attempts yet
Problem
You are given a sequence of positive integers. You may remove any numbers from the sequence to make it "friendly". A sequence is friendly if there is an integer () such that every number in the sequence is a multiple of . Since the empty sequence is friendly, it is always possible to make the initial sequence friendly.
There may be several ways to make the sequence friendly, so you decide to maximize the sum of all numbers in the friendly sequence. Compute the maximum possible sum of all numbers in a friendly sequence obtained from the initial sequence.
Input
The input consists of a single test case formatted as follows. The first line contains a single integer (). The -st line contains an integer () for .
Output
Print the maximum sum of all numbers in a friendly sequence obtained from the initial sequence.