#P7244. Maximizing Condensation in Chapters
Maximizing Condensation in Chapters
Maximizing Condensation in Chapters
You are given a sequence of n positive integers a1, a2, ..., an which represent the "conceptual values" of materials. You need to partition the sequence exactly into k contiguous and non-empty segments (chapters). For the i-th segment which covers indices [li, ri], define its chapter value bi as the maximum element in that segment, i.e.,
\( b_i = \max_{j \in [l_i, r_i]} \{a_j\} \)
Then, the condensation of the entire composition is defined as the greatest common divisor (GCD) of these chapter values:
\( \gcd(b_1, b_2, \ldots, b_k) \)
Your task is to determine the maximum possible condensation that can be obtained by partitioning the sequence into exactly k segments optimally.
inputFormat
The first line contains two integers n and k (1 ≤ k ≤ n), representing the number of materials and the number of chapters respectively.
The second line contains n positive integers a1, a2, ..., an, where ai represents the conceptual value of the i-th material.
outputFormat
Output a single integer — the maximum possible condensation (i.e. the maximum value of \( \gcd(b_1, b_2, \ldots, b_k) \) ) that can be achieved by an optimal partition.
sample
5 2
4 6 8 2 10
4