前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CodeForces 732B Cormen — The Best Friend Of a Man

CodeForces 732B Cormen — The Best Friend Of a Man

作者头像
ShenduCC
发布2018-04-27 11:02:07
1.1K0
发布2018-04-27 11:02:07
举报
文章被收录于专栏:算法修养算法修养

B. Cormen — The Best Friend Of a Man

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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, if?k?=?5and yesterday Polycarp went for a walk with Cormen?2?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 of?n?integers?a1,?a2,?...,?an, where?ai?is the number of times Polycarp will walk with the dog on the?i-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 next?n?days so that Cormen will feel good during all the?n?days. You can assume that on the day before the first day and on the day after the?n-th day Polycarp will go for a walk with Cormen exactly?k?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 the?i-th day.

Input

The first line contains two integers?n?and?k?(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 next?n?days so that Cormen will feel good during all days.

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

Examples

input

代码语言:javascript
复制
3 5
2 0 1

output

代码语言:javascript
复制
4
2 3 2

input

代码语言:javascript
复制
3 1
0 0 0

output

代码语言:javascript
复制
1
0 1 0

input

代码语言:javascript
复制
4 6
2 4 3 5

output

代码语言:javascript
复制
0
2 4 3 5
代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>

using namespace std;
int n,k;
int a[505];
int b[505];
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int ans=0;
    for(int i=2;i<=n;i++)
    {
        if(a[i]+a[i-1]>=k) continue;
        else
        {
             ans+=(k-a[i]-a[i-1]);
              a[i]+=(k-a[i]-a[i-1]);
        }


    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++)
    {
        if(i!=n)
            printf("%d ",a[i]);
        else
            printf("%d\n",a[i]);
    }
    return 0;

}
本文参与?腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-11-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com