#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
freopen("in2.txt","r",stdin);
int n,k,a[500],b[500],c[500];
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int i,j,p,q,ans=-200010;
for(int l=1;l<=n;l++)
{
for(int r=l;r<=n;r++)
{
for(p=1,i=l;i<=r;i++)b[p++]=a[i];
for(q=1,j=1;j<l;j++)c[q++]=a[j];
for(j=r+1;j<=n;j++)c[q++]=a[j];
sort(b+1,b+p);
sort(c+1,c+q);
//for(int i=1;i<p;i++)printf("%d ",b[i]);printf("\n");
//for(int i=1;i<q;i++)printf("%d ",c[i]);printf("\n");
for(int i=1,j=q-1;i<=k&&i<p&&j>=1;i++,j--)
{
if(c[j]>b[i])swap(b[i],c[j]);
else break;
}
int s=0;
for(int i=1;i<p;i++)
{
s+=b[i];
}
//printf("s=%d\n",s);
ans=max(ans,s);
}
}
printf("%d\n",ans);
return 0;
}
A. Sereja and Swaps
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation:
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.
What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations?
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
10 2 10 -1 2 2 2 2 2 2 -1 10
output
32
input
5 10 -1 -1 -1 -1 -1
output
-1
用了扬神的方法,暴力枚举区间,排序交换k次