#P9032. Maximum Skyscraper Beauty
Maximum Skyscraper Beauty
Maximum Skyscraper Beauty
You are given a row of n skyscrapers with heights \(h_1, h_2, \dots, h_n\). Domagoj will capture a contiguous subarray of these skyscrapers, but he only wants to photograph at least k skyscrapers. The beauty of a photo capturing skyscrapers in the interval \([l, r]\) is defined as:
[ \text{Beauty} = \gcd\left(h_l, h_{l+1}, \dots, h_r\right) \times \left(\sum_{i=l}^{r} h_i\right) ]
Your task is to help Domagoj choose a subarray (with at least k skyscrapers) which maximizes the beauty value.
Input Format
The first line contains two integers n and k (\(1 \le k \le n\)). The second line contains n integers \(h_1, h_2, \dots, h_n\) where each \(h_i\) is the height of the i-th skyscraper.
Output Format
Output a single integer, the maximum beauty value of any subarray with at least k skyscrapers.
inputFormat
The input begins with a line containing two space-separated integers \(n\) and \(k\). The next line contains \(n\) space-separated integers representing the heights of the skyscrapers: \(h_1, h_2, \dots, h_n\).
outputFormat
Output a single integer — the maximum beauty value computed as:
[ \text{Beauty} = \gcd\left(h_l, h_{l+1}, \dots, h_r\right) \times \left(\sum_{i=l}^{r} h_i\right) ]
for any subarray \([l, r]\) with \(r-l+1 \ge k\).
sample
5 2
3 6 8 5 2
28