#K36592. Longest Common Divisor Subsequence

    ID: 25788 Type: Default 1000ms 256MiB

Longest Common Divisor Subsequence

Longest Common Divisor Subsequence

Given a list of integers, let \(M = \max\{a_1, a_2, \ldots, a_n\}\) be the maximum element in the list. Your task is to find and output the longest subsequence (preserving the original order) such that every element \(a_i\) in the subsequence is a divisor of \(M\), i.e. \(M \bmod a_i = 0\). If the list is empty, output an empty line.

For example, if the list is [1, 2, 4, 5, 10], the maximum element is 10. The numbers 1, 2, 5 and 10 all divide 10 evenly, so the output should be 1 2 5 10 (notice that 4 is skipped as 10 % 4 ≠ 0).

This problem tests your basic knowledge of number theory and array manipulation.

inputFormat

The input is read from stdin and consists of two parts:

  1. An integer n representing the number of elements in the list.
  2. If n > 0, a second line containing n space-separated integers.

outputFormat

Output a single line to stdout that contains the elements of the longest common divisor subsequence separated by a single space. If the input list is empty, output an empty line.

## sample
4
1 3 6 24
1 3 6 24

</p>