K-Max Sum

Description
Here there is a very classical problem --- Max Sum!
Let's review the classsical problem:
Given a sequence a[1], a[2], a[3] ...... a[N], your job is to calculate the max sum of a
sub-sequence and the sub-sequence's length must equals M.
But today, before you do the calculations, you are allowed to execute at most K consecutive
operations. every operation, you can select an arbitrary number from the sequence and multiply it
by 2. In addition, you must know that each number can only multiply once!
See example for more details! Input
There are some test cases.
For each test case: There is a N, M (1 <= M <= N <= 100,000) - the number of elements in the
sequence and the length of the chosen sub-sequence of the sequence!
Then N integers followed. (all the integers are between -10^9 and 10^9).The third line contains a
single integer K (0 <= K <= N) — the maximum allowed number of operations! Output

Output the answer for the above problem.

 Sample Input 

3  3 

1 2 3 

2


5  3

1 -2 3 -4 5 

2

 Sample Output

11 

12

#include <stdio.h>
#include <algorithm>
using namespace std;
  
int a[100005];
int p[100005];
int list1[100005];
int lm;
bool cmp( int a, int b)
{
     if (a>b) return true ;
     return false ;
}
int main()
{
     int c,i,n,m,k,t;
long long  b,max1,_max;
     while ( scanf ( "%d %d" ,&n,&m)!=EOF)
     {
         for (i=0;i<n;i++)
         {
             scanf ( "%d" ,&a[i]);
             if (a[i]>0)
                 a[i]+=a[i];
         }
         scanf ( "%d" ,&k);
         b=0;
         for (i=0;i<m;i++)
         {
             b+=a[i];
         }
         max1=b;
         lm=0;
         list1[lm++]=0;
         t=n-m+1;
         for (i=1;i<t;i++)
         {
             b=b-a[i-1]+a[i-1+m];
             if (b>max1)
             {
                 max1=b;
                 lm=0;
                 list1[lm++]=i;
             }
             else if (b==max1)
             {
                 list1[lm++]=i;
             }
         }
         _max=max1;
         max1=0;
         for (c=0;c<lm;c++)
         {
             b=_max;
             for (i=0;i<m;i++)
                 p[i]=a[i+list1[c]];
             sort(p,p+m,cmp);
             for (t=k;p[t]>0&&t<m;t++)
             {
                 b-=p[t]/2;
             }
             if (b>max1)
                 max1=b;
         }
         printf ( "%lld\n" ,max1);
     }
     return 0;
}
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值