Codeforces Round #243 (Div. 1) A Sereja and Swaps
题目描述
A. Sereja and Swaps
As usual, Sereja has array a, its elements are integers: a[1],a[2],...,a[n]. Let’s introduce notation:
$$
f \left( a, l, r \right) = \sum_{i=l}^{r} a \left[ i \right]; \ m \left( a \right) = \underset{1 \leq l \leq r \leq n}{max} f \left( a, l, r \right)
$$
A swap operation is the following sequence of actions:
- choose two indexes
i, j (i ≠ j); - perform assignments
tmp = a[i], a[i] = a[j], a[j] = tmp.
Input
The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], …, a[n] ( - 1000 ≤ a[i] ≤ 1000).
Output
In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.
Sample test(s)
Input
1 | 10 2 |
Output
1 | 32 |
Input
1 | 5 10 |
Output
1 | -1 |
解题思路
穷举所有范围内能取得的最大值,对于某一范围,将此范围内的数据单独拿出并排序,再把此范围外的数据存入另一数组并排序,在k次之内,每次拿后者的最大值a替换前者的最小值b(前提是a>b)。
代码
1 |
|