#P10878. Maximizing Final Number with Limited Maximum Operations
Maximizing Final Number with Limited Maximum Operations
Maximizing Final Number with Limited Maximum Operations
You are given a sequence (a_1, a_2, \dots, a_n) of length (n). You must perform exactly (n-1) operations to reduce the sequence to a single number. There are two types of operations defined for any consecutive sequence (b_1, b_2, \dots, b_l) (of length (l)):
- Operation One (Max Operation): Replace the sequence with (c_1, c_2, \dots, c_{l-1}) where (c_i = \max(b_i, b_{i+1})) for all (1 \le i < l).
- Operation Two (Min Operation): Replace the sequence with (c_1, c_2, \dots, c_{l-1}) where (c_i = \min(b_i, b_{i+1})) for all (1 \le i < l).
You are allowed to use Operation One at most (m) times ((0 \le m \le n-1)); the remaining operations (i.e. (n-1-m) operations) must be Operation Two. You can choose the order in which you apply these operations arbitrarily.
Determine the maximum possible final number (the single element remaining after all (n-1) operations) that you can achieve by choosing the order of operations optimally.
Note: All formulas are in (\LaTeX) format as shown above.
inputFormat
The first line contains two integers (n) and (m) (where (n) is the length of the sequence and (m) is the maximum number of times you can use Operation One). The second line contains (n) space‐separated integers (a_1, a_2, \dots, a_n).\n\nConstraints: (2 \le n \le 50) (for example) and (0 \le m \le n-1).
outputFormat
Output a single integer denoting the maximum final number you can achieve after applying exactly (n-1) operations with at most (m) Operation Ones.
sample
3 1
5 3 4
5