Gym - 100513F Ilya Muromets 贪心

Силачом слыву недаром — семерых одним ударом!

From the Russian cartoon on the German fairy tale.

Ilya Muromets is a legendary bogatyr. Right now he is struggling against Zmej Gorynych, a dragon with n heads numbered from 1 to n from left to right.

Making one sweep of sword Ilya Muromets can cut at most k contiguous heads of Zmej Gorynych. Thereafter heads collapse getting rid of empty space between heads. So in a moment before the second sweep all the heads form a contiguous sequence again.

As we all know, dragons can breathe fire. And so does Zmej Gorynych. Each his head has a firepower. The firepower of the i-th head is fi.

Ilya Muromets has time for at most two sword sweeps. The bogatyr wants to reduce dragon's firepower as much as possible. What is the maximum total firepower of heads which Ilya can cut with at most two sword sweeps?

Input

The first line contains a pair of integer numbers n and k (1 ≤ n, k ≤ 2·105) — the number of Gorynych's heads and the maximum number of heads Ilya can cut with a single sword sweep. The second line contains the sequence of integer numbers f1, f2, ..., fn (1 ≤ fi ≤ 2000), where fi is the firepower of the i-th head.

Output

Print the required maximum total head firepower that Ilya can cut.

Examples

Input

8 2
1 3 3 1 2 3 11 1

Output

20

Input

4 100
10 20 30 40

Output

100

题解:一个人可以砍两刀,每刀可以消去连续的K个数,然后问你两刀最多能砍下数的和是多少

题解:题意就相当于取两个连续k个,所以从后往前更新,没更新k个,维护连续k个的最大值,更新此时前面dp[i]

dp[i] 表示 取当前连续k个 和 后面连续k个的最大值

坑点:如果2*k>=n 直接输出总和

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll a[maxn];
ll dp[maxn];
int main()
{
    ll n,k;
    while(~scanf("%lld%lld",&n,&k))
    {
        ll sum1=0;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            sum1+=a[i];
        }
        if(2*k>=n)
        {
            printf("%lld\n",sum1);
            continue;
        }
        ll j=n,cnt=0,sum=0,maxx=0;
        for(ll i=n;i>=1;i--)
        {
            sum+=a[i];
            cnt++;
            if(cnt>=k)
            {
                maxx=max(maxx,sum);
                dp[j]+=sum;     // 加上当前连续k个的值
                dp[i-1]=maxx; // 赋给前面最大值
                sum-=a[j];
                j--;
            }
        }
        maxx=0;
        for(ll i=0;i<n;i++)
            maxx=max(maxx,dp[i]);
        printf("%lld\n",maxx);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/84557116
今日推荐