1. 程式人生 > >Gym - 100513F Ilya Muromets 貪心

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