传送门

Description

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 integers b1, 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.

Sample Input

3 52 0 1
3 10 0 0
4 62 4 3 5

Sample Output

42 3 2
10 1 0
02 4 3 5

思路

题意:

给出一串数,要求前后连续的两个数的和不小于k,问每个数在符合要求的情况下,最少需要加多少,输出最后的串的值。

官方题解:

If we don't make enough walks during days i and i + 1, it's better to make an additional walk on day i + 1 because it also counts as a walk during days i + 1 and i + 2 (and if we walk one more time on day i, it won't help us in the future). So we can start iterating from the second day (1"=indexed). We will add max(0, k - ai - ai - 1) walks to the day i (and to our answer), so Cormen has enough walks during days i and i - 1. After we have iterated through all days, we can print the answer.

Time complexity: O(n).

#include<bits/stdc++.h>
using namespace std;
const int maxn = 505;

int main()
{
	int n,k,sum = 0,a[maxn];
	scanf("%d%d",&n,&k);
	for (int i = 0;i < n;i++)	scanf("%d",&a[i]);
	for (int i = 1;i < n;i++)	sum += max(0,k-a[i-1]-a[i]),a[i] +=max(0,k-a[i-1]-a[i]);
	printf("%d\n",sum);
	printf("%d",a[0]);
	for (int i = 1;i < n;i++)	printf(" %d",a[i]);
	printf("\n");
	return 0;
}

  

Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)的更多相关文章

  1. Codeforces Round #377 (Div. 2) D. Exams

    Codeforces Round #377 (Div. 2) D. Exams    题意:给你n个考试科目编号1~n以及他们所需要的复习时间ai;(复习时间不一定要连续的,可以分开,只要复习够ai天 ...

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

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

  3. Codeforces Round #377 (Div. 2)

    #include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...

  4. Codeforces Round #377 (Div. 2)D(二分)

    题目链接:http://codeforces.com/contest/732/problem/D 题意: 在m天中要考k个课程, 数组a中有m个元素,表示第a[i]表示第i天可以进行哪门考试,若a[i ...

  5. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. Codeforces Round #377 (Div. 2) E. Sockets

    http://codeforces.com/contest/732/problem/E 题目说得很清楚,每个电脑去插一个插座,然后要刚好的,电脑的power和sockets的值相同才行. 如果不同,还 ...

  7. Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟

    http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...

  8. Codeforces Round #377 (Div. 2) 被坑了

    http://codeforces.com/contest/732/problem/B 题目要求任意两个连续的日子都要 >= k 那么如果a[1] + a[2] < k,就要把a[2]加上 ...

  9. Codeforces Round #377 (Div. 2)部分题解A+B+C!

    A. Buy a Shovel 题意是很好懂的,一件商品单价为k,但他身上只有10块的若干和一张r块的:求最少买几件使得不需要找零.只需枚举数量判断总价最后一位是否为0或r即可. #include&l ...

随机推荐

  1. #研发解决方案#discache-分布式缓存查询与管理系统

    郑昀 基于马海元和闫小波的文档 关键词:memcached.redis.分布式缓存.控制台.反序列化.Java 本文档适用人员:研发和运维员工 提纲: 如何查看缓存里的序列化数据? 批量删除来一个 监 ...

  2. ORACLE Linux以及 Unbreakable Enterprise Kernel

    Oracle Linux,全称为Oracle Enterprise Linux,简称OEL,Linux发行版本之一.Oracle公司在2006年初发布第一个版本,以对Oracle软件和硬件支持较好见长 ...

  3. Symantec Backup Exec Agent For Linux防火墙问题

    如果在Unix或Linux安装配置好了Symantec Backup Exec Agent For Linux,但是在Symantec Backup Exec服务端无法访问Symantec Backu ...

  4. HDFS开发实例

    1.列出HDFS中的文件 package com.hdfs.test; import java.io.BufferedReader; import java.io.IOException; impor ...

  5. web.xml is missing and <failOnMissingWebXml> is set to true 错误解决办法

    对web项目的解决方案: 右击项目——>Java EE Tools——>Generate Deployment Descriptor Stub. 然后系统会在src/main/webapp ...

  6. 0027 Java学习笔记-面向对象-(非静态、静态、局部、匿名)内部类

    内部类 内部类就是把一个类写在另一个类的内部 用途: 如果一个类只希望它被某一个类访问,那么可以把它定义在另一个类的内部,并用private修饰 内部类可以访问它所在外部类的private成员:但所在 ...

  7. ubuntu下新建用户

    ubuntu下新建用户和RedHat系列的linux有点不一样 新建用户的命令是useradd,修改密码是passwd,如下: sudo useradd zhuhui sudo passwd 1234 ...

  8. hortonworks2.5.3 install step log

    1.创建本地YUM源,下载TAR.GZ HDP,HDP-UTILS,AMBARI介质安装HTTPD,在/VAR/WWW/HTML/下创建三个相应的目录,把以上解压的三个目录链接过来在三个目录中执行命令 ...

  9. jstl中的sql:query标签获取的结果如何格式化输出

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. 在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)

    说明: 程序使用 io.h 中的 _findfirst 和 _findnext 函数遍历文件夹,故而程序只能在 Windows 下使用. 程序遍历当前文件夹,对其中的文件夹执行递归遍历.同时检查遍历到 ...