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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fLL
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn];
ll ans;
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n)
{
if(a[i]<m-a[i-])
{
ans+=m-a[i-]-a[i];
a[i]=m-a[i-];
}
}
printf("%lld\n",ans);
rep(i,,n)printf("%d ",a[i]);
//system("Pause");
return ;
}

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

  1. 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 ...

  2. CodeForces 732B Cormen — The Best Friend Of a Man

    B. Cormen - The Best Friend Of a Man time limit per test 1 second memory limit per test 256 megabyte ...

  3. 【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 ...

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

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

  5. 算法导论 (Thomas H.Cormen / Charles E.Leiserson / Ronald L.Rivest / Clifford Stein 著)

    第一部分 基础知识 第1章 算法在计算中的作用 第2章 算法基础 第3章 函数的增长 第4章 分治策略 第5章 概率分析和随机算法 第二部分 排序和顺序统计量 第6章 堆排序 第7章 快速排序 第8章 ...

  6. OpenCASCADE Interpolation - Lagrange

    OpenCASCADE Interpolation - Lagrange eryar@163.com Abstract. Power basis polynomial is the most simp ...

  7. 数据结构与算法 Big O 备忘录与现实

    不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新.        算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...

  8. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏

    three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...

  9. c/c++常用网址

    个人主页Dennis Ritchie's home pagehttp://cm.bell-labs.com/cm/cs/who/dmr/index.html Brian Kernighan's hom ...

随机推荐

  1. 【解题报告】瑞士轮(NOIP2011普及组T3)

    [题外话:这道题吧……说实话我不太喜欢……因为卡快排.] 题目不贴了,就是给你一个赛制,然后各个选手的初始得分和能力值,问你进行R轮比赛之后第Q名的编号是多少(这个编号读进来就要算OYZ,初始快排的时 ...

  2. javascript基础(二)类型转换

    原文http://pij.robinqu.me/ 类型转换 当期望使用一个布尔值的时候,可以提供任意类型值,JavaScript将根据需要自行转换类型.类型转换可以分为隐式转换和显式转换. 显式转换 ...

  3. 可靠通信的保障 —— 使用ACK机制发送自定义信息——ESFramework 通信框架4.0 快速上手(12)

    使用ESPlus.Application.CustomizeInfo.Passive.ICustomizeInfoOutter接口的Send方法,我们已经可以给服务端或其它在线客户端发送自定义信息了, ...

  4. Excel教程(13) - 统计函数

    AVEDEV 用途:返回一组数据与其平均值的绝对偏差的平均值,该 函数可以评测数据(例如学生的某科考试成绩)的离散度. 语法:AVEDEV(number1,number2,...) 参数:Number ...

  5. Matlab - 线性方程组求解

    常用函数:det 计算矩阵的行列式的值inv 求矩阵的逆阵rank 求矩阵的秩[V D]=eig(A) 求矩阵A的特征值和特征向量poly 求矩阵的特征多项式rref 用初等变换将矩阵化成行阶梯形nu ...

  6. Vultr优惠码20美元享受20GB SSD和2T流量

    美国vps主机商vultr最新优惠码:20FOR30,只限新注册用户,点击注册链接,进入后台Billing,找到Gift code,输入20FOR30,点击apply,就能免费获得20美元的账户余额. ...

  7. 洛谷-语文成绩-[有奖]洛谷5月月赛:kkksc03的三大神器

    题目背景 Background语文考试结束了,成绩还是一如既往地有问题. 题目描述 Description语文老师总是写错成绩,所以当她修改成绩的时候,总是累得不行.她总是要一遍遍地给某些同学增加分数 ...

  8. SharePoint 入门书籍推荐 转载来源http://www.cnblogs.com/jianyus/p/3513238.html

    最近,总有人说刚入门SharePoint,没有好的资料或者电子书,资料推荐大家多看看博客园和CSDN的博客.对于看博客,我一般是两个思路,要么找一个人的从头到尾看一遍,觉得有意义的,就把地址加收藏:或 ...

  9. NSBundle、UIImageView和UIButton对比、Xcode文档安装路径、Xcode模拟器安装路径

    1.NSBundle1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹2> 利用mainBundle就可以访问软件资源包中的任何资源3> 模拟器应用程序 ...

  10. 常用 ajax js 表单

    $(function () { /** * 图片点击放大处理 */ $('.mini_img').click(function () { $(this).hide().next().show(); } ...