Codeforces Round #243 (Div. 2) C. Sereja and Swaps

#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
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值