Gym - 100513G FacePalm Accounting 贪心

Statements

An owner of a small company FacePalm has recently learned that the city authorities plan to offer to small businesses to participate in improving parks and garden squares. However, credible sources informed the FacePalm owner that the loss-making companies will not get such an offer. Moreover, the sources have also told how loss-making companies will be determined.

A company will be considered loss-making if for every k contiguous days the total income of the company is negative.

The FacePalm owner discussed the situation with his chief accountant, and they decided to change the report so that the company would look loss-making.

The company report for n days can be represented as a sequence of integers a1, a2, ..., an, where ai is the company income in the day i (negative values correspond to losses).

The accountant can change any values in this sequence, but no updated value can become less than the smallest value in the original report — otherwise the updated report will look absolutely implausible. Besides, the accountant wants the total change of the values to be as small as possible.

We will assume that the total change of the values is , where is the i-th value in the updated report.

Your task is to calculate the minimum required total change of the values and provide the updated report.

Input

The first line contains integers n and k (1 ≤ k ≤ n ≤ 2·105) — the number of days in the report and the number of days in the definition of loss-making company.

The second line contains n space-separated integers a1, a2, ..., an ( - 10000 ≤ ai ≤ 10000), where ai is the company income in day i.

It is guaranteed that at least one value of ai is negative.

Output

In the first line print the required minimum total change. In the second line print the corresponding updated report. No value in the updated report can be less than the minimum value of the original report.

If there are multiple solutions, print any of them.

Examples

Input

5 4
3 -3 -1 1 2

Output

1
2 -3 -1 1 2

Input

8 3
2 1 -3 -2 4 -3 0 2

Output

3
1 1 -3 -2 2 -3 0 2

Input

4 2
-2 1 -2 1

Output

0
-2 1 -2 1

Input

3 3
-5 6 10

Output

12
-5 -5 9

题意:对此序列任意的K长度区间,需满足和小于0,问你最小修改差是多少,且修改的值不得小于原序列中最小值

题解:先吧前k个操作成合适的,若原始就不符合,就优先减后面的,维护连续k个数的和,若大于等于0了,操作最后一个数,保证连续k个小于等于0

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=2e5+10;
ll a[N];
int n,k;
int main()
{

    while(~scanf("%d%d",&n,&k))
    {
        ll sum=0,minn=1e9;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            minn=min(minn,a[i]);
            if(i<=k) sum+=a[i];
        }
        ll ans=0;
        if(sum>=0)
        {
            ans+=(sum+1);
            for(int i=k;i>=1&&sum>=0;i--)
            {
                if(a[i]>minn)
                {
                    if(sum>=(a[i]-minn))
                    {
                        sum-=(a[i]-minn);
                        a[i]=minn;
                    }
                    else
                    {
                        a[i]-=(sum+1);
                        sum=-1;
                    }
                }
            }
        }
        for(int i=k+1,j=1;i<=n;i++,j++)
        {
            if(sum-a[j]+a[i]>=0)
            {
                ans+=(sum-a[j]+a[i]+1);
                a[i]-=(sum-a[j]+a[i]+1);
                sum=-1;
            }
            else
                sum=sum-a[j]+a[i];
        }
        printf("%lld\n",ans);
        for(int i=1;i<=n;i++)
            printf("%lld%c",a[i]," \n"[i==n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/84556572