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. 使用Angular构建单页面应用(SPA)

    什么是SPA?看下图就是SPA: 下面说正经的,个人理解SPA就是整个应用只有一个页面,所有的交互都在一个页面完成,不需要在页面之间跳转. 单页面的好处是更快的响应速度,更流畅的用户体验,甚至和桌面应 ...

  2. PHP中使用CURL(四)

    为了安全,我们的web服务主机往往不能上网.维护的时候,也是通过跳板机,ssh登录后去操作.有时候我们的程序需要访问外网.比如需要调用外网其他程序的某个接口.这时可以通过PHP的CURL函数的CURL ...

  3. apicloud ios 打包流程

    1创建APP ID 等 2创建pruduction创建证书,用 这个证书导出P12格式的.. 3profile 证书,选 APP STORE  AD-HOC ok... http://docs.api ...

  4. 斯坦福大学公开课:iOS 7应用开发 笔记

    2015-07-06 第一讲   课务.iOS概述 -------------------------------------------------- 开始学习斯坦福大学公开课:iOS 7应用开发留 ...

  5. app测试--稳定性测试

    稳定性测试的概念有2种, 一, 稳定性测试,对应于异常性测试,即发生异常情况时,系统如何反应的测试.包含: 1 交互性测试,被打扰的情况,如来电,短信,低电量等.这些其实在上章的功能测试中有提到. 2 ...

  6. JQuery_图片未加载!

    JQuery_图片未加载! <html> <head> <script type="text/javascript" src="/jquer ...

  7. spring中的控制反转IoC和依赖注入DI

    原文:http://blog.163.com/xianghuxian@126/blog/static/50639037200721345218382/ IoC(Inversion of Control ...

  8. 【3】docker命令集

    root@xcc-VirtualBox:/home/xcc# docker --helpUsage: docker [OPTIONS] COMMAND [arg...]       docker [ ...

  9. VB.net结束进程

    Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...

  10. WebDriver使用IE和chrome浏览器

    因为我用的是 selenium-dotnet-2.47.0.zip IEDriverServer_x64下载地址 https://code.google.com/p/selenium/download ...