Codeforces Round #377 (Div. 2) b. cormen-the best Friend of a man (greedy)

Source: Internet
Author: User

Tag: des exit + + continuous complex minimum mil ble const

Portal

Description

Recently a dog is bought for Polycarp. The dog ' s name is cormen. Now Polycarp have a lot of troubles. For example, Cormen likes going for a walk.

K  walks for any of the 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.

n  days and made a sequence Of  n  integers  a 1, a 2, ..., a n , where  a i  is the number of times Polycarp would walk With the dog on The  I -th Day and doing all his affairs (for example, he have to go t o A shop, throw out the trash, etc.).

n  days so that Cormen would feel good Duri ng all The  n  days. You can assume, the the day before the first day, and on the day after The  n -th Day Polycarp'll go for a walk with Cormen exactly  K  times.

Write a program that'll find the minumum number of additional walks and the appropriate schedule-the sequence of Integ ERs b1, b2, ..., bn (bia i), where bi means the total number of walks with the dog on the i-th day.

Input

The first line contains integers n and k (1≤ n, k ≤500)-the number of days and the minimum number of walks with cormen for any, and 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 does during the next N Day s so that cormen'll feel good during all days.

n  integers  b 1, b 2, ..., b n , Where  b i  -the total number of Walks on The  i -th Day according to the found Solutions ( a i b i  for all  i  from 1 to  n ). If There is multiple solutions, print any of them.

Sample Input
3 5
2 0 1
3 1
0 0 0

4 6
2 4 3 5
Sample Output
4
2 3 2

1
0 1 0

0
2 4 3 5
Ideas

Test instructions

Given a series of numbers, requires a continuous two numbers and not less than K, ask each number in accordance with the requirements of the case, the minimum need to add how much, the output of the last string value.

Official:

If we don ' t make enough walks during daysIandI+ 1, it's better to make a 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'll add Max(0, K - ai - ai -1) walks To the day i (answer), so cormen have enough walks during days i and I -1. After we had 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", & amp;n,&k), for (int i = 0;i < n;i++) scanf ("%d", &a[i]), and 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]);p rintf ("%d\n", sum);p rintf ("%d", a[0]), for (int i = 1;i < n;i++) printf ("%d", A[i]);p rintf (" \ n "); return 0;}

  

Codeforces Round #377 (Div. 2) b. cormen-the best Friend of a man (greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.