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
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;
}