You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?
Definitions of subsegment and array splitting are given in notes.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105) — the size of the array a and the number of subsegments you have to split the array to.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).
Output
Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.
Examples
Input
5 2
1 2 3 4 5
Output
5
Input
5 1
-4 -5 -3 -2 -1
Output
-5
Note
A subsegment [l, r] (l ≤ r) of array a is the sequence al, al + 1, ..., ar.
Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).
In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.
In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand the problem. We have array a of n integers, and we must split it into exactly k contiguous non-empty subsegments that together cover the whole array (i.e., a partition into k contiguous blocks). For each block, compute the minimum value in that block. Then take the maximum over those k minima. We need to maximize this value by choosing the split points appropriately.
Goal: max over splits of min_i per block, then take max. This is a classic problem: maximize the minimum of k subarray minima? Actually it's: we want to maximize the maximum of the minima? Wait: The objective: For a given split, compute min of each block, then take the maximum among those minima. So it's like we want the largest value that can be guaranteed as the minimum of at least one block, given we can partition the array into k blocks. We can think: We want to choose split such that the smallest number in each block is as high as possible, but then we take the maximum of these minima, which is essentially the highest among the block minima. But we can design splits to isolate a large element as its own block (so its min = that element), while the other blocks might have smaller minima. Since we take the maximum over minima, we only care about the largest block minimum. So the maximum possible value is simply the maximum element of the array? Not exactly because we need to form exactly k blocks. Could we always isolate the maximum element in its own block? That would require that we can split so that one block is just that element. But we need exactly k blocks total. That's possible if we can isolate the maximum element into a singleton block, and the rest of the array into k-1 blocks (which is possible as long as the rest array can be partitioned into k-1 non-empty contiguous segments, i.e., n-1 >= k-1 -> n >= k). Since k <= n, that holds. So we could set the max element alone as one block, and then arbitrarily split the rest into k-1 blocks (maybe they have small minima, but we don't care because we take the maximum among minima, which would be the max element). So the answer would be the maximum element of the whole array? But wait, example 1: array [1,2,3,4,5], k=2. Maximum element is 5, answer is 5, matches. Example 2: array [-4,-5,-3,-2,-1], k=1. Only one block must be whole array, its minimum is -5, answer -5. The maximum element is -1, but answer is -5, not -1. Because with k=1, we cannot isolate the max element; we must have only one block which is the whole array. So the answer is just the minimum of the whole array, which is -5. So indeed, when k=1, answer is min(a). When k>=2, can we always achieve the global maximum? Let's test with an example: n=3, k=2, a=[5,1,5]. Max is 5. Can we get answer 5? Yes: split as [5] and [1,5] gives minima: 5 and 1, max = 5. Or [5,1] and [5] gives minima:1 and5, max=5. So works.
Consider k=2, array [1,2,3]? max=3, can we get 3? Split [1,2] and [3] gives minima: 1 and3 => max=3. Good.
So it seems when k>=2, we can always isolate the maximum element? But careful: What if k > 2? Example: n=3, k=3, array [1,2,3]. Then we have to split into 3 blocks, each element individually. The minima are 1,2,3, max=3 which is the max element. So yes.
What about array with duplicate maximums? Still works.
Thus it seems answer is max element for k>=2, and min element for k=1. But wait, is there any nuance where we cannot isolate the max element because we need exactly k blocks, and perhaps isolating the max element forces some block to be empty? Since we must cover all elements, and we can place the max element alone as a block, the rest of the array (n-1 elements) must be partitioned into k-1 non-empty blocks. This requires n-1 >= k-1, i.e., n >= k, which is given. So always possible. So for k>=2, answer = max(a). However, is there any scenario where the global maximum might not be achievable because the max element is at an edge and k=2? But still we can put the max as a singleton block: if max at position i, we can split as [1..i] and [i+1..n] (if i<n) or [1..i-1] and [i..n] (if i>1) to isolate it. For i=1: we can split as [1] and [2..n]; that yields a block containing max as only element, min = max, and another block covering rest. For i=n: split as [1..n-1] and [n]. So works.
Thus answer = max(a) for k>=2, and min(a) for k=1. But wait, there is also the possibility that k is exactly n? Then each block is a single element, minima = each element, max = max(a). So fine.
But let's double-check problem statement carefully: "You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?" So it's a typical "maximin" or "minimax" but actually we are maximizing the max of mins, which is just the maximum element when we can isolate it. So answer appears trivial.
But wait, there is perhaps a twist: The subsegments must be contiguous and cover the whole array, but the order is preserved. The objective is to maximize the maximum among the minima. To get the maximum element as the max of minima, we need to ensure that the block containing that element has its minimum equal to that element, meaning that block must contain no element smaller than that element. Since the element is the maximum overall, any other element is <= it. However, if the block contains a smaller element, the minimum of that block becomes the smaller element, not the max. So to have the block's minimum equal to the max element, the block must consist solely of that element (or possibly contain elements that are >= it, but that's impossible because it's the global max, so any other element is <= it, so if it contains any other element, the min could be that other element if it's smaller. But if the other element is equal to it (also max), then min would still be that max value. For instance, if the array has two elements both equal to the max value M, then a block containing both M's will have min M. So it's not necessary that the block must be singleton; it could contain multiple copies of the max value and no smaller elements. However, if there is any element smaller than M in the same block, the min becomes smaller. So to have a block min = M, the block must not contain any element less than M. So we need a block that contains only elements that are >= M. But since M is the maximum, that means all elements in that block must be exactly M (or maybe also other values equal to M? Actually if there are elements larger than M, impossible because M is max. So they must be equal to M). So the block must consist exclusively of maximum-valued elements. So we need a contiguous segment containing only max elements. If the maximum elements are not contiguous, we cannot have a block that includes only them without including some lower-valued elements in between. Because the block is contiguous; it cannot skip elements. So if the maximum value appears at positions that are separated by lower values, we cannot form a block containing multiple maxes without also including the lower values. So perhaps we cannot always achieve the global maximum as the block's minimum, unless we can isolate a single max element or a contiguous region of max values.
Wait, this is critical: In order to get a block whose minimum equals M, the block must have all elements >= M. Since M is the global maximum, any element other than M is strictly less than M (assuming all other values are smaller). So the block must contain only elements that are M. So the block must be a contiguous segment consisting entirely of M's. If there is at least one occurrence of M, we can take a block that is exactly that occurrence alone (size 1). That's always contiguous and contains only M. So that works. So we don't need multiple maxes; a singleton block works.
Thus it seems fine.
But is there any scenario where we cannot isolate an occurrence of M into a singleton block due to the requirement of exactly k blocks? We already argued we can always isolate it as one block and the rest as k-1 blocks, provided n-1 >= k-1, i.e., n >= k, which is true. So always possible.
Thus the answer is indeed max(a) for k>=2, min(a) for k=1.
But wait, let's test a more subtle example: n=2, k=2, a=[-10, -5]. max = -5, answer? We can split into [-10] and [-5] => minima: -10 and -5, max = -5. So it's max. For k=2, answer is -5, which is indeed max. For k=1, answer = min = -10.
So far so good.
But I'm aware of a known Codeforces problem: "Split the array into k segments and maximize the minimum of the maximums?" Something like that. But here it's "max of mins". Actually there is a problem "Maximum of Minimums" or something. I recall a problem: "Maximize the minimum among segment maximums" which is more complex. But this one seems too simple. Could there be a twist? Let's re-read the problem statement exactly as given: "You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums." So it's max of mins. Yes.
I can imagine a trick: When k=1, we take min of whole array. When k>=2, we can always get the global max by making it its own segment. But wait, the problem might have constraints that a_i can be negative, zero, positive. So no issue.
But is there any scenario where we cannot isolate the global max because we need exactly k segments and the max element might be forced to be in a segment with a smaller element due to some requirement? Let's think: We can choose the split points arbitrarily. To isolate an element at position i, we can set a split before i and after i. For example, if we want a segment [i, i], we need to set boundaries at i-1 and i. That is allowed as long as we have enough segments to cover the rest. For k segments, we will have k-1 split points among the n-1 gaps. So we need to allocate these split points. We can always put splits at positions i-1 and i (or just after i) to isolate it. However, if k>2, we have more splits, we can still allocate splits accordingly.
Let's formalize: To isolate a specific element at position i as its own segment, we need to cut before i (i.e., between i-1 and i) and after i (between i and i+1). That uses up two cuts (if i not at ends, we need both cuts; if i=1, we need only cut after 1; if i=n, cut before n). So number of cuts needed depends. But we have exactly k-1 cut points (since splitting into k segments requires choosing k-1 cut positions among the n-1 gaps). Actually we need to decide where to cut. To get a singleton at i, we must cut at the gap before i (i.e., between i-1 and i) unless i=1. And cut at the gap after i (i.e., between i and i+1) unless i=n. So we need up to 2 cuts. But we can also get a singleton with just one cut if i is at one end. But overall, we can always allocate cuts to achieve that as long as we have enough cuts to also partition the rest. Let's see: Total number of cuts is k-1. If we want to isolate i as singleton, we must ensure that we cut at the necessary gaps. However, we might need to use up to 2 cuts for that. That's fine because k-1 can be as small as 1 when k=2. If k=2, we have exactly 1 cut. Can we isolate a singleton with only 1 cut? Suppose we want to isolate element at position i. If i=1 or i=n, then a single cut suffices: for i=1, cut after 1 gives segment1=[1], segment2=[2..n]. For i=n, cut before n gives segment1=[1..n-1], segment2=[n]. If i is in the middle, to isolate it as its own segment with only one cut, we can't because we would need two cuts to separate it from both sides. With only one cut, you can only split the array into two contiguous parts; one part will contain i and possibly some others. So if k=2 and i is interior, we cannot make it a singleton? Let's think: For k=2, we have exactly one split point: choose a split point s (1 <= s < n). The segments are [1..s] and [s+1..n]. To have a segment that is just element i, we need either [1..s] = [i] and s=i, then second segment is [i+1..n]; or [s+1..n] = [i] and s+1=i, i.e., s=i-1, then first segment is [1..i-1]. So indeed, we need i to be at the split boundary. So i must be at position 1? Actually if s=i, then first segment ends at i, second starts at i+1. The first segment includes all elements up to i; that's not a singleton unless i=1. If s=i and i=1, first segment is [1] (singleton) and second is [2..n]. That works. If we want the singleton to be the second segment, we need s+1 = i, i.e., s = i-1. Then second segment starts at i, but second segment is [i..n]; that includes i and everything after, not just i, unless i=n. So for second segment to be singleton, we need i = n and s = n-1. So indeed, with k=2, the only possible singletons are at the ends. So if the maximum element is not at either end, we cannot isolate it as a singleton when k=2. However, we might still get a block with min = max if the block consists of only max elements, but if max appears only at an interior position and there are no other max elements adjacent, any block containing that element will also contain some other elements unless we cut both sides, which requires two cuts (i.e., k>=3). So for k=2, we might not be able to achieve max if the maximum is interior and not at an end. But wait, is there a way to get the max as the max of minima without isolating it? Consider array: [1,100,2], k=2. Maximum is 100. Can we achieve answer 100? We need to split into two segments. Options: split after 1: segments [1] (min=1) and [100,2] (min=2) => max = max(1,2)=2. split after 2: segments [1,100] (min=1) and [2] (min=2) => max=2. So answer = 2, not 100. So indeed, we cannot achieve 100. So our earlier assumption that k>=2 always yields max(a) is false! This counterexample shows it's not always possible. So the answer is not simply max(a) for k>=2.
Thus we need to solve the problem properly.
Let's reconsider: For k=2, as we just saw, answer is not max unless max at an end. For k>2, perhaps we can isolate an interior element by using multiple cuts. For example, array [1,100,2], k=3: split into [1],[100],[2] -> minima: 1,100,2 -> max = 100. So with k=3, we can isolate interior max. So for k>=3, can we always isolate any element as a singleton? To isolate an interior element as singleton, we need at least two cuts: one before it and one after it. That requires at least 3 segments? Actually to get a singleton interior, you need three segments: left part, singleton, right part. So you need at least k=3. If k>=3, you can isolate any element as a singleton? But also need to ensure total segments exactly k, which may be more than 3; we can always further split the left or right parts to reach exactly k segments. So yes, if k>=3, we can isolate any element. But is there any condition on n? For k=3, we need n>=3 to have 3 segments; that's given. So with k>=3, we can always isolate any specific element as a singleton as long as we have enough elements to split. For interior element, we need to cut before and after. That uses two cuts, leaving k-2 more cuts for the remaining parts. Since we can always further split the left and right segments arbitrarily (they can be split into any number of subsegments as long as total segments sum to k). So for k>=3, we can always achieve max(a) by isolating a maximum element as a singleton. But careful: If the maximum element appears at an interior position, we need to cut before and after it. That yields three segments: left, singleton max, right. That's exactly three segments. If k>3, we can split left or right further to get total k. So yes, it's always possible if k>=3. But what about the scenario where there are multiple max elements but not contiguous? Isolating one of them as singleton still works. So for k>=3, answer = max(a).
Wait, test: n=2, k=3 not possible because k <= n. So k cannot be > n. So for n=2, k can only be 1 or 2. So for n=2, k=2: we already saw interior? For n=2, there is no interior; both ends. So if max is at either position, we can isolate it if it's at an end? For k=2, we can isolate an end. Since one of the positions is an end, the max is at either first or second. So we can isolate it by making it a singleton? For array [100,1], k=2: split after first gives [100] and [1] => minima 100 and 1, max=100. So works. For array [1,100], split after first gives [1] and [100]? Actually split after 1: segments [1] and [2..2] which is [100] -> yes, max=100. So for n=2, k=2, we can always isolate max because max is at one of the ends (since there are only two positions, both ends). So for n=2, it's fine. For n=2, k=2, answer = max. For n=3, k=2, we have interior element (position 2). So if max is at position 2, we cannot isolate it. So answer might be something else. So we need to compute the answer for k=2 specifically.
Now, what about k=1? Answer = min(a). So the only non-trivial case is k=2. Possibly also k > 2? But we argued for k>=3 answer = max(a). Let's double-check for k=3 with n=3 and max at position 2. Works: [a1], [a2], [a3] -> max. So yes.
But is there any scenario where k>=3 but we cannot isolate the max because the array length is exactly k and the max is at interior? For n=k, each segment must be exactly one element if we want to isolate max? Actually for n=k, we must have each segment of length 1. That's allowed, because we have exactly n segments and each is a single element. Then minima are the elements themselves, max = max(a). So it's fine. So for k>=3, answer = max(a). The only potential nuance is if the maximum element is not unique and we might need to ensure that there is a contiguous block of max values? But we can pick any occurrence; a singleton is enough. So yes.
Thus the problem reduces to:
If k == 1: answer = minimum of the whole array.
If k == 2: we need to compute the maximum possible value of max(min of left segment, min of right segment) over all splits. Since we only have two segments, we need to consider the best possible split point.
If k >= 3: answer = maximum element of array.
But wait: Could there be a scenario where k>=3 but the answer is not max? Suppose the array has negative infinity? But still max is the maximum. That's fine.
But maybe there is a nuance: For k>=3, is it always optimal to isolate the max? Let's consider a tricky case: array = [5, 1, 5], k=2? That's not >=3. For k=3, we can have [5], [1], [5] => minima:5,1,5 => max=5, which is the max. So fine.
But could there be a case where the max is so high that it's impossible to get it as a min because even if we isolate it, its min is itself, but then the other segments might have minima that are even higher? Actually we care about max of minima. So if we isolate max, we get a min = max. That's as high as possible, cannot exceed the max of array. So it's optimal.
Thus the solution is: if k>=3, print max(a); if k==1, print min(a); if k==2, compute something else.
Now for k=2: we must consider all split points s from 1 to n-1 (since we need two non-empty segments). For each split, we have left segment = a[1..s], right segment = a[s+1..n]. Compute L_min = min(a1..as), R_min = min(a_{s+1}..a_n). Then the objective value for this split = max(L_min, R_min). We need to maximize this over s.
We can precompute prefix minima and suffix minima. Let prefix_min[i] = min(a1..ai). suffix_min[i] = min(ai..an). Then for split after i (i from 1 to n-1), value = max(prefix_min[i], suffix_min[i+1]). We need the maximum over i.
But is that all? Let's test on example [1,100,2] from earlier: prefix_min: [1,1,1? Actually careful: indices: a1=1, a2=100, a3=2. prefix_min[1]=1, prefix_min[2]=min(1,100)=1, prefix_min[3]=min(1,100,2)=1. suffix_min[1]=min(1,100,2)=1, suffix_min[2]=min(100,2)=2, suffix_min[3]=2. For i=1 (split after first): L_min=1, R_min=suffix_min[2]=2 => max=2. i=2: L_min=prefix_min[2]=1, R_min=suffix_min[3]=2 => max=2. So answer = 2. That matches.
What about array [5,1,5]? i=1: L_min=5, R_min=min(1,5)=1 => max=5. i=2: L_min=min(5,1)=1, R_min=5 => max=5. So answer=5, which is max. But wait, max=5, answer=5, but we said for k=2 sometimes max is not achievable if max interior? In [5,1,5], max=5 appears at both ends. So we can isolate an end. So answer=5. Good.
What about array where max appears only in interior and not at ends, e.g., [1,10,1,1]? i=1: L_min=1, R_min=min(10,1,1)=1 => max=1; i=2: L_min=min(1,10)=1, R_min=min(1,1)=1 => max=1; i=3: L_min=min(1,10,1)=1, R_min=1 => max=1. Answer=1, which is not 10. So indeed, max not achievable.
But could there be a case where the answer for k=2 is something in between? Possibly. So we need to compute that.
Thus the overall algorithm:
Read n,k, array a.
If k==1: ans = min(a).
Else if k>=3: ans = max(a).
Else (k==2): compute prefix minima and suffix minima, then for i in 1..n-1: candidate = max(pref_min[i], suff_min[i+1]), ans = max over candidates.
But is there any other possibility? Could there be a scenario where k>=3 but we might get an even larger value than max(a)? No, because max(a) is the maximum element, and any min of a segment is at most the maximum element of that segment, which is <= max(a). So answer cannot exceed max(a). So max(a) is optimal.
But wait, could there be a scenario where the max element is so high that isolating it yields a min of that, but perhaps there is some other split where the max of mins is also max(a)? Not higher.
Thus answer for k>=3 is straightforward.
But I should double-check if k=2 might also be handled by a simpler rule: answer is max(first element, last element)? Let's test: [1,100,2] first=1, last=2, max=2, matches. [5,1,5] first=5, last=5, max=5 matches. [1,10,1,1] first=1, last=1, max=1 matches. [2,3,1] first=2, last=1 => max=2? Check splits: i=1: L_min=2, R_min=min(3,1)=1 => max=2; i=2: L_min=min(2,3)=2, R_min=1 => max=2. So answer=2, which matches max(first,last). Let's test another: [3,1,2] first=3, last=2 => max=3? Compute splits: i=1: L_min=3, R_min=min(1,2)=1 => max=3; i=2: L_min=min(3,1)=1, R_min=2 => max=2 => max over splits = max(3,2)=3. So answer =3, which is max(first,last). How about [1,2,3,4]? first=1, last=4 => max=4. Splits: i=1: L_min=1, R_min=min(2,3,4)=2 => max=2; i=2: L_min=min(1,2)=1, R_min=min(3,4)=3 => max=3; i=3: L_min=min(1,2,3)=1, R_min=4 => max=4. So max over splits = 4 = max(first,last). [4,3,2,1] first=4, last=1 => max=4. Splits: i=1: L_min=4, R_min=min(3,2,1)=1 => max=4; i=2: L_min=min(4,3)=3, R_min=min(2,1)=1 => max=3; i=3: L_min=min(4,3,2)=2, R_min=1 => max=2 => answer =4 = max(first,last). [2,5,3,1,4] first=2, last=4 => max=4? Let's compute: splits: i=1: L_min=2, R_min=min(5,3,1,4)=1 => max=2; i=2: L_min=min(2,5)=2, R_min=min(3,1,4)=1 => max=2; i=3: L_min=min(2,5,3)=2, R_min=min(1,4)=1 => max=2; i=4: L_min=min(2,5,3,1)=1, R_min=4 => max=4 => answer =4, which equals max(first,last)=4? Actually last=4, first=2, max=4. [5,4,3,2,1] first=5, last=1 => max=5 => answer should be 5? Let's compute splits: i=1: L_min=5, R_min=min(4,3,2,1)=1 => max=5; i=2: L_min=min(5,4)=4, R_min=min(3,2,1)=1 => max=4; i=3: L_min=min(5,4,3)=3, R_min=min(2,1)=1 => max=3; i=4: L_min=min(5,4,3,2)=2, R_min=1 => max=2; answer=5 = max(first,last). [1,3,2] first=1, last=2 => max=2? Compute splits: i=1: L_min=1, R_min=min(3,2)=2 => max=2; i=2: L_min=min(1,3)=1, R_min=2 => max=2; answer=2 = max(first,last). [3,2,1] first=3, last=1 => max=3? Splits: i=1: L_min=3, R_min=min(2,1)=1 => max=3; i=2: L_min=min(3,2)=2, R_min=1 => max=2; answer=3 = max(first,last). [2,1,3] first=2, last=3 => max=3? Splits: i=1: L_min=2, R_min=min(1,3)=1 => max=2; i=2: L_min=min(2,1)=1, R_min=3 => max=3; answer=3 = max(first,last). [1,2,1] first=1, last=1 => max=1? Splits: i=1: L_min=1, R_min=min(2,1)=1 => max=1; i=2: L_min=min(1,2)=1, R_min=1 => max=1; answer=1 = max(first,last). [2,1,2] first=2, last=2 => max=2. So far it appears that for k=2, the answer is simply max(a[0], a[n-1]). Let's test more random arrays to see if this holds always.
Intuition: For a split at position i, the left min is the minimum of prefix up to i. As i moves from 1 to n-1, prefix min is non-increasing (actually prefix minima are non-increasing? Since prefix min[i] = min(prefix_min[i-1], a_i). So it is decreasing or staying same. Similarly, suffix min[i+1] is non-decreasing as i increases? Let's see: suffix_min[j] = min(suffix_min[j+1], a_j). As j decreases (i increasing), suffix_min[i+1] is the min of suffix starting at i+1. As i increases, the suffix starts later, so its min can either stay same or increase (since you are dropping elements from the left of the suffix). So suffix_min is non-decreasing as we move i forward (i.e., as the starting index increases). So we have two sequences: pref_min[i] (which is decreasing as i increases? Actually prefix min is non-increasing: it can only go down or stay same. As i increases, we add more elements, so min can only get smaller or stay same. So it's a non-increasing sequence (monotonic decreasing or equal). Suffix_min[i+1] is non-decreasing as i increases (since the set of elements in suffix shrinks from the left, so minimum can increase or stay same). So the max of these two values for each i: we are taking max of a non-increasing and a non-decreasing sequence. The maximum over i of max(L,R) is likely achieved at one of the endpoints: i=1 or i=n-1. Because as i moves from 1 to n-1, L goes down (or stays), R goes up (or stays). The function f(i)=max(L(i),R(i)): Initially, at i=1, L(1)=a1 (or min of first element) = a1, R(1)=suffix_min[2] = min of all except first. At i=n-1, L(n-1)=prefix_min[n-1] = min of all except last, R(n-1)=a_n. The max of the two sequences might be high at either end. But could there be a case where the maximum occurs in the middle? Suppose L is high initially, then decreases, and R is low initially, then increases. The max of them is the larger of the two. Since L is high at start and decreasing, R is low at start and increasing. So the max might be high at the left if L dominates, and at the right if R dominates. At some point they might cross. At the crossing, the max is roughly the crossing value. But that crossing value is likely between the two endpoint values? However, could it be that both endpoints have lower values than some interior? For that to happen, we would need that at some interior i, both L and R are larger than at both ends? But L(i) <= L(1) because L is non-increasing, so L(i) can't be larger than L(1). Similarly, R(i) >= R(1)? Wait, R is non-decreasing, so R(i) >= R(1). So R(i) is at least R(1). So at interior, L(i) <= L(1) and R(i) >= R(1). Therefore max(L(i),R(i)) is at least max(L(1), R(1))? Not necessarily: If L(1) is large and R(1) is small, max(L(1),R(1)) = L(1). At interior, L(i) <= L(1), R(i) >= R(1). Since L(i) <= L(1), the max could be either L(i) if L(i) >= R(i) or R(i) if R(i) > L(i). Could it be that R(i) becomes greater than L(1) while L(i) is less than L(1)? Then max(L(i),R(i)) = R(i) which might be > L(1). But L(1) is the max at left endpoint. So interior could give a larger value if R(i) exceeds L(1). However, note that R(i) at interior i is the min of suffix from i+1 to n. As i increases, R(i) becomes the min of a smaller set, which could be larger. It's possible that at some i, R(i) becomes larger than a1 (L(1)). Then max at that i would be R(i), which could be > a1. But is R(i) <= a_n? Not necessarily; suffix min at i+1 could be a_n if a_n is the smallest of that suffix. Actually, suffix min of the tail could be any element, but as we drop elements, the minimum may increase. For example, consider array [5,1,4]. Then a1=5, a_n=4. Compute L(1)=5, R(1)=min(1,4)=1 => max=5. L(2)=min(5,1)=1, R(2)=4 => max=4. So max over i is 5 (left endpoint). So interior not higher.
Consider array [3,100,2]. a1=3, a_n=2. L(1)=3, R(1)=min(100,2)=2 => max=3. L(2)=min(3,100)=3? Wait min(3,100)=3, R(2)=2 => max=3. Actually, prefix_min[2] = min(3,100)=3, suffix_min[3]=2. So max=3 both, answer=3. But could we get a value higher than 3? Not because max element is 100 but not achievable. So answer = max(a1, a_n) = max(3,2)=3.
Now consider array where a1 is small, a_n is small, but interior suffix min becomes large? For R(i) to be large, the suffix from i+1 must have a high minimum, i.e., all elements in that suffix are large. But the suffix includes the last element, so if a_n is small, then suffix min will be at most a_n, because a_n is part of the suffix for any i < n. Actually if i < n, suffix from i+1 includes a_n. So suffix_min[i+1] is at most a_n. Because the minimum over a set that includes a_n is <= a_n. So R(i) <= a_n. Similarly, L(i) is at most a_1? Actually L(i) includes a1, so L(i) <= a1. Wait, L(i) is min of prefix up to i, includes a1, so L(i) <= a1. So both L(i) and R(i) are bounded above by a1 and a_n respectively? More precisely: For any i, L(i) = min( a1, ... , ai ) ≤ a1. So L(i) ≤ a1. Similarly, R(i) = min( a_{i+1}, ... , a_n ) ≤ a_n. Therefore, max(L(i), R(i)) ≤ max(a1, a_n)? Because if L(i) ≤ a1 and R(i) ≤ a_n, then max(L(i),R(i)) ≤ max(a1, a_n). However, is it always true that R(i) ≤ a_n? Since a_n is part of the suffix, the minimum of the suffix is at most a_n, indeed min(suffix) ≤ a_n. But we want an upper bound for max(L,R). Because L ≤ a1, R ≤ a_n, so the maximum cannot exceed max(a1, a_n). So indeed, max over splits is at most max(a1, a_n). Conversely, can we achieve max(a1, a_n)? At split i=1, we have L=a1, R ≤ a_n, so max = a1 if a1 >= R, which might be less than a1? Actually at i=1, R = suffix_min[2] ≤ a_n. So max = max(a1, something ≤ a_n). If a1 >= that something, max = a1. If a1 < that something? But something ≤ a_n, and a1 might be less than a_n. Could it be that a1 is less than a_n and the something is a_n? Actually suffix_min[2] could be equal to a_n if a_n is the minimum of the suffix (which includes all except first). But if a_n is the smallest element among positions 2..n, then suffix_min[2] = a_n. Then max(a1, a_n) = a_n if a_n > a1? But max(a1, a_n) is either a1 or a_n. At i=1, max = max(a1, a_n) if suffix_min[2] = a_n and a_n > a1? Then max = a_n. But if suffix_min[2] is less than a_n, then max = max(a1, something < a_n). Could that something be > a1? Possibly, but since something ≤ a_n, if a_n > a1, it could be that something is, say, 5, a1=3, a_n=10, and suffix_min[2]=5 (because maybe there is a smaller element 5 among positions 2..n, but there is also a_n=10 which is larger, min is 5). Then max = max(3,5)=5, which is less than max(a1, a_n)=10. But maybe at i=n-1, we get L = prefix_min[n-1] which ≤ a1, R = a_n, so max = max(prefix_min[n-1], a_n). Since prefix_min[n-1] ≤ a1, max ≤ max(a1, a_n). Actually prefix_min[n-1] could be less than a1, but if a_n is the larger, max = a_n. So at i=n-1, we always get max = max(prefix_min[n-1], a_n). Since prefix_min[n-1] is some value ≤ a1, the max is at least a_n (if a_n is larger than prefix_min[n-1]) else prefix_min[n-1] which is ≤ a1. So max at i=n-1 might be a_n or something less. If a_n is the maximum of a1 and a_n (i.e., a_n >= a1), then max at i=n-1 is at least a_n? Actually if a_n >= a1, but prefix_min[n-1] might be less than a_n, then max = a_n. So we achieve a_n. If a1 > a_n, then at i=1, max = a1 if suffix_min[2] ≤ a1? Actually we need to check if we can always achieve max(a1, a_n). Let's test a scenario where a1=3, a_n=10, but suffix_min[2] is something like 5, and prefix_min[n-1] is something like 2. Then at i=1, max = max(3,5)=5; at i=n-1, max = max(2,10)=10. So we get 10, which is max(a1,a_n). So it seems we can always achieve at least max(a1, a_n) by taking the split at the appropriate end: if a1 is larger, split at i=1? Actually if a1 is larger than a_n, then at i=1, L=a1, R= suffix_min[2] ≤ a_n ≤ a1, so max = a1. So we get a1. If a_n is larger, then at i=n-1, L = prefix_min[n-1] ≤ a1 ≤ a_n, and R = a_n, so max = a_n. So indeed, we can achieve max(a1,a_n). But what if a1 and a_n are equal? Then either gives that value.
Thus the maximum possible value for k=2 is exactly max(a[0], a[n-1]). Because we just argued that any split yields max(L,R) ≤ max(a[0], a[n-1]) due to L ≤ a[0] and R ≤ a[n-1]. And we can achieve exactly max(a[0], a[n-1]) by choosing the split at the appropriate end. Therefore, answer for k=2 is simply max(first element, last element). That is much simpler than scanning all splits. However, careful: Is it always true that L(i) ≤ a[0]? Yes, because prefix minimum includes a[0] (index 1). So the min of any prefix cannot exceed the first element? Actually "cannot exceed" in terms of value? Since it's the minimum, it is less than or equal to each element, so certainly ≤ a1. So L(i) ≤ a1. Similarly, R(i) ≤ a_n. So max(L(i),R(i)) ≤ max(a1, a_n). And we can achieve max(a1, a_n) by the split at the appropriate end: if a1 >= a_n, split after first element => L = a1, R = min of rest ≤ a_n ≤ a1, so max = a1. If a1 < a_n, split before last => L = min of first n-1 ≤ a1 < a_n, R = a_n, so max = a_n. So yes.
Thus for k=2, answer = max(a1, an). This is a neat simplification.
We should test with the earlier array [1,100,2] where a1=1, a_n=2, max=2. That matches our computed answer. [1,10,1,1] a1=1, a_n=1 => max=1, matches. [5,1,3] a1=5, a_n=3 => max=5; compute splits: i=1: L=5, R=min(1,3)=1 => max=5; i=2: L=min(5,1)=1, R=3 => max=3; so answer=5. Good.
Thus overall solution:
If k == 1: output min(a)
Else if k == 2: output max(a[0], a[n-1])
Else: output max(a)
But wait: What about k > n? Not allowed, since k ≤ n. So safe.
Now, check edge cases:
n=1, k=1: answer = a[1] (since min and max are same). According to our rules: k==1 => min(a) = a[1] correct. If n=1, k cannot be >1 because k ≤ n, so only k=1.
n=2, k=2: answer = max(a1, a2). That's correct. Indeed split after first yields both singletons; max of mins = max(a1,a2).
n=2, k=1: answer = min(a1,a2). Works.
n=3, k=2: answer = max(a1, a3). We've seen that works except maybe if interior max is higher, but can't achieve it. So correct.
n=3, k=3: answer = max(a). Works.
Thus the problem reduces to O(n) reading + O(1) answer.
But is there any scenario where k=2 but the optimal split yields a value larger than max(a1, an)? According to our bound, no, because L(i) ≤ a1 and R(i) ≤ a_n. But could L(i) be greater than a1? No, because L(i) = min of prefix; since a1 is part of prefix, the minimum is at most a1. So L(i) ≤ a1. Could R(i) be greater than a_n? No, because a_n is part of suffix, so min ≤ a_n. So indeed max(L(i), R(i)) ≤ max(a1, a_n). So it's an absolute upper bound, and we can achieve it.
Thus the solution is correct.
But wait, I recall a similar Codeforces problem: "Split in Three" or "Maximum splitting" with minima. I think the solution indeed is this. So the answer is trivial after reasoning.
But we need to be careful about negative numbers? No difference.
Thus final answer.
Now implement accordingly.
But before finalizing, we should double-check a tricky case: array = [100, 1, 100] with k=2. a1=100, an=100 => max=100. We can achieve that by splitting after first (L=100, R=min(1,100)=1 => max=100). So answer 100, correct.
Complexities: O(n) to read and compute min, max, and first/last elements.
Edge Cases: n=1, k=1 => output that element.
Thus we can produce solution.
Now, let's write the solution accordingly.
But before final, we need to consider if the problem statement might require outputting an integer, and we need to handle large inputs (up to 1e5). So we can do.
Now, let's draft the solution:
#include
#include
#include
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector a(n);
for (int i=0; i<n; ++i) cin >> a[i];
if (k == 1) {
int ans = a[0];
for (int x : a) if (x < ans) ans = x;
cout << ans << endl;
} else if (k == 2) {
int ans = max(a[0], a[n-1]);
cout << ans << endl;
} else {
int ans = a[0];
for (int x : a) if (x > ans) ans = x;
cout << ans << endl;
}
return 0;
}
But we can also compute min and max in one pass.
Alternatively, use *min_element and *max_element.
But careful: For k >= 3, we output max element. For k == 2, output max(first, last). For k == 1, output min element.
Now, think about case where n=2, k=2: [5,1] => output max(5,1)=5 -> correct.
Thus final solution.
But wait, is there any hidden nuance when k > 2? Could there be any scenario where the answer is not max(a) because we have to split into exactly k subsegments and maybe if k > 2 but we cannot isolate the max due to constraints on the number of segments? We argued we can isolate any element if we have at least 3 segments because we need to cut before and after. But if n is small, e.g., n=3, k=3 we can isolate all as singletons. For n=4, k=3, we can isolate an interior element as singleton: Suppose we want to isolate a[2] as singleton. Then we need segments: [1], [2], [3..4] (or [1..2]? Wait, to isolate a[2], we need to cut after index1 (between 1 and2) and after index2 (between2 and3). That gives three segments: [1], [2], [3..n]. That's exactly 3 segments. So works. For k>3, we can further split any segment. So yes, any interior element can be isolated if k>=3. But what about n=2, k=2? That's covered. n=2, k cannot be 3.
Thus all good.
But could there be an issue with arrays where all elements are the same? Then max=min, answer same regardless.
Thus the solution is correct.
But let's double-check the bound proof for k=2: We claimed L(i) ≤ a1 and R(i) ≤ a_n, thus max(L,R) ≤ max(a1,a_n). This is valid because a1 is part of the prefix, so min(prefix) ≤ a1; but wait, min(prefix) ≤ a1, but is it always less than or equal? Yes, min of a set that includes a1 is at most a1 (it could be equal if a1 is the smallest, or less if there is a smaller). So L(i) ≤ a1. Similarly, R(i) ≤ a_n. So indeed max(L,R) ≤ max(a1,a_n). Good.
Now, we can achieve max(a1,a_n) by splitting at the appropriate end. But we must check: In the case where a1 >= a_n, splitting after first gives L = a1? Actually L = min(prefix up to 1) = a1 (since only a1). R = min(suffix from 2) ≤ a_n. Since a_n ≤ a1, R ≤ a1, so max = a1. So achieved.
In case a_n > a1, splitting before last (i=n-1) gives L = min(prefix up to n-1) ≤ a1 < a_n, and R = a_n, so max = a_n. So achieved.
Thus answer for k=2 is max(a1, a_n).
Therefore, we can implement as described.
Now, this problem likely expects this solution; many participants might have been tricked into thinking it's more complex.
Now we will produce final answer.
But wait, what about the case where k=2 and n=1? n must be at least k, so n=1 only k=1. So no issue.
Thus final.
Now, let's write the solution in Python for clarity, but the problem might be in any language. We'll produce Python code.
Implementation details:
Read n, k.
Read list a of length n.
If k == 1: print(min(a))
elif k == 2: print(max(a[0], a[-1]))
else: print(max(a))
Time O(n). Space O(1).
Now, confirm with sample inputs.
Example 1: 5 2 => a = [1,2,3,4,5] => max(1,5)=5 output 5. Ok.
Example 2: 5 1 => min = -5 output -5.
Now, test additional:
3 2: [1,100,2] => output max(1,2)=2. That seems plausible.
Now, let's think about potential pitfalls: Input values can be large, up to 1e9, but that's fine. Use int in Python.
Now, I'll consider the possibility of negative numbers: For k=2, first and last could be negative, max chooses the larger (less negative). That works.
Now, we should also check if there is any scenario where the answer might be something else for k>=3? Possibly if n=1, k=1, else for n>=2, k>=3, answer = max. But what about n=2, k>=3 not allowed. So fine.
Thus, final answer.
But wait, what about k=0? Not allowed.
Thus solution is clear.
Now, we can produce final response.
But I want to double-check the original problem's note: "Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark)." So that's exactly contiguous cover.
Thus, final answer.
Now, I'll produce the final answer with explanation.
Given the nature of the platform, I'll output the solution code.
But the user asked: "You are given an array ... What is the maximum possible integer you can get?" So I need to provide the answer (explanation and code). They likely want code solution.
Thus, I'll produce a final answer with explanation and code.
Now, I should consider any other edge: Might there be a scenario where k=2 and n=2 but the answer is not max(a1,a2) but something else? For n=2, the only split is after first, giving segments [a1] and [a2]. Then min of each segment is a1 and a2, max is max(a1,a2). So yes.
Thus correct.
Now, I'm ready to produce final output.
But wait, let's consider the possibility that for k=2, the answer is max of first and last, but is there any scenario where splitting at interior gives same max but also allows larger? No.
Thus final.
Now, I'll answer.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
k = int(next(it))
a = [int(next(it)) for _ in range(n)]
code
if k == 1:
ans = min(a)
elif k == 2:
# The best we can do is the larger of the first and the last element
ans = max(a[0], a[-1])
else: # k >= 3
ans = max(a)
print(ans)