Cormen — The Best Friend Of a Man (贪心)

来源:互联网 发布:最近很火的网络歌曲女 编辑:程序博客网 时间:2024/05/16 08:16

Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk.

Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, ifk = 5 and yesterday Polycarp went for a walk with Cormen2 times, today he has to go for a walk at least 3 times.

Polycarp analysed all his affairs over the next n days and made a sequence ofn integers a1, a2, ..., an, whereai is the number of times Polycarp will walk with the dog on thei-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).

Help Polycarp determine the minimum number of walks he needs to do additionaly in the nextn days so that Cormen will feel good during all then days. You can assume that on the day before the first day and on the day after then-th day Polycarp will go for a walk with Cormen exactlyk times.

Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integersb1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on thei-th day.

Input

The first line contains two integers n andk (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days.

The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.

Output

In the first line print the smallest number of additional walks that Polycarp should do during the nextn days so that Cormen will feel good during all days.

In the second line print n integers b1, b2, ..., bn, wherebi — the total number of walks on thei-th day according to the found solutions (ai ≤ bi for alli from 1 to n). If there are multiple solutions, print any of them.

Example
Input
3 52 0 1
Output
42 3 2
Input
3 10 0 0
Output
10 1 0
Input
4 62 4 3 5
Output
02 4 3 5

题意:

要使任意连续两天的遛狗次数和大于k

求: 在所给的遛狗情况中还需要额外遛狗的次数,以及每天的遛狗次数

代码:

#include<stdio.h>#include<string.h>using namespace std;const int N = 505;int arr[N];int main(){int n,m;scanf("%d%d",&n,&m);for(int i=0;i<n;i++)scanf("%d",&arr[i]);int sum=0;for(int i=0;i<n-1;i++){if(arr[i]+arr[i+1]<m){sum+=m-arr[i]-arr[i+1];arr[i+1]=m-arr[i];}}printf("%d\n",sum);    for(int i=0;i<n-1;i++)    printf("%d ",arr[i]);    printf("%d\n",arr[n-1]);return 0;}


原创粉丝点击