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
3 5
2 0 1
output
4
2 3 2
input
3 1
0 0 0
output
1
0 1 0
input
4 6
2 4 3 5
output
0
2 4 3 5

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

CodeForces 732B Cormen — The Best Friend Of a Man的更多相关文章

  1. CodeForces 732B Cormen — The Best Friend Of a Man (贪心)

    题意:给定n和k表示,狗要在任意连续两天散步次数要至少为k,然后就是n个数,表示每天的时间,让你增加最少次数使得这个条件成立. 析:贪心,策略是从开始到最后暴力,每次和前面一个相比,如果相加不够k,那 ...

  2. 【56.74%】【codeforces 732B】Cormen --- The Best Friend Of a Man

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. Codeforces Round #377 (Div. 2)A,B,C,D【二分】

    PS:这一场真的是上分场,只要手速快就行.然而在自己做的时候不用翻译软件,看题非常吃力非常慢,还有给队友讲D题如何判断的时候又犯了一个毛病,一定要心平气和,比赛也要保证,不要用翻译软件做题: Code ...

  4. Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)

     传送门 Description Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. drawer navigation, tabhostFragment 默认导向

    问题: 项目结构,在一个抽屉导航中的第一个抽屉中加入了一个tabHostFragment,每次进入项目都是在抽屉导航的默认界面,须要把抽屉划出来再选择某个抽屉. 可是想让APP直接跳转到第一个抽屉界面 ...

  2. Entity Framework底层操作封装V2版本号(2)

    这个类是真正的数据库操作类.上面的那个类仅仅是调用了这个封装类的方法进行的操作 using System; using System.Collections.Generic; using System ...

  3. ubuntu下载linux内核源码

    ubuntu仓库里面关于源码部分配置的好全啊,什么都有,ps:包括vim的各种插件居然也有人打包放到仓库里,真是方便. 1.首先查看一下本系统使用的内核版本号: cat /proc/version L ...

  4. Qt学习过程中遇到的问题

    由于工作需要,开始使用Qt,由于在网上找的教程文档时针对qt3的,所以在学习的过程遇到了许多由于版本不一致造成的问题,因此记录下来. 参考的文档是:Qt入门教程 详细讲解版 本机Qt版本为:Qt5.3 ...

  5. python学习笔记(9)--Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法

    Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法 这篇文章主要介绍了Python UnicodeEncodeErro ...

  6. RabbitMQ之HelloWorld【译】

    简介 RabbitMQ是一个消息代理,主要的想法很简单:它接收并转发消息.你可以把它当做一个邮局,当你发送邮件到邮筒,你相信邮差先生最终会将邮件投递给收件人.RabbitMQ在这个比喻里,是一个邮筒, ...

  7. http://jadethao.iteye.com/blog/1926525

    http://jadethao.iteye.com/blog/1926525 ————————————————————————————————————————————————————————————— ...

  8. OAuth2.0 介绍

    一.基本协议流程: (1) Client请求RO(Resource Owner)的授权:请求中一般包含:要访问的资源路径,操作类型,Client的身份等信息.(2) RO批准授权:并将“授权证据”发送 ...

  9. gsoap 学习 1-自己定义接口生成头文件

    接口头文件的格式在向导中没有看到明确的说明性的内容,但通过看开发包中示例程序中头文件定义和通过wsdl生成的头文件的内容,可以发现,头文件中都会出现以下几行信息 //gsoap ns service ...

  10. Web设计的速查卡(转)

    一.排版 1.VisiBone Font Card Download: GIF 2.常用字体表 (HTML) 3.混合字体 (PDF) 二.单元/尺寸 1.Points 和Pixels近似转换表 (H ...