AmazonOA #83
Calculate Minimum Cost (Divisible Resource Replacement)
HardJudge-ready
Topics (AI-labelled)Bits & MathHashing
You are given a positive integer array cost. Each operation chooses two positions x and y; when the current cost[x] is divisible by the current cost[y], you may set cost[x]=cost[y]. Any number of operations may be performed, including zero; find the minimum possible sum of the final elements. Values can only be copied from existing positions, and new values such as a gcd cannot be created; duplicate values count toward the sum at each of their positions. The answer 17 given for the public MDX input [3,6,2,6,25] is wrong; under the stated operation it should be 34; only the complete FastPrep input [3,6,2,5,25] has the answer 17.
Input
The first line contains n, and the second line contains n integers cost[i]. 1≤n≤2000000, 1≤cost[i]≤200000000.
Output
Output the minimum possible sum as a 64-bit integer.
Example 1
Input:
5
3 6 2 6 25
Output:
34
Example 2
Input:
5
3 6 2 5 25
Output:
17
Example 3
Input:
3
6 10 15
Output:
31
Example notes
In the first example, the two 6s become 2 and 25 cannot be lowered, so the sum is 34. In the second example, 6 becomes 2 and 25 becomes 5, so the sum is 17. In the third example, 6, 10, 15 do not divide one another, so the sum stays 31; they cannot be turned into the nonexistent gcd=1.
Editorial
Loading solution…