Prefix GCD Maximization
Practice
4.3 (3 votes)
Dynamic programming
Dynamic programming and bit masking
Introduction to dynamic programming 1
Algorithms
Bitmask
1 D
Problem
81% Success 879 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

You are given an array of size \(N\). Rearrange it in such a manner that the sum of the GCD over all the non-empty prefixes of the array is maximum.

GCD here means greatest common divisior. It is the largest number which divides any set of numbers without leaving any remainder with any of them.

Input format:

  • The first line contains a single integer \(N\).
  • The second line, contains \(N\) space-separated integers, the array elements. 

Output format:

Print the answer — the maximum possible sum of all the prefix GCD's of the rearranged array.

Constraints:

\(1 \leq N \leq 20 \\ 1 \leq A_i \leq 10^9 \\ \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
26 votes
Tags:
AlgorithmsDynamic ProgrammingMedium
Points:30
5 votes
Tags:
AlgorithmsDynamic ProgrammingMedium
Points:30
14 votes
Tags:
AlgorithmsApprovedBit ManipulationDynamic ProgrammingMedium