Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/gym/100513/problem/G
Description
An owner of a small company Facepalm have recently learned that the city authorities plan for offer to small businesses to P Articipate in improving parks and garden squares. However, credible sources informed the Facepalm owner that the loss-making companies would not get such a offer. Moreover, the sources has also told how loss-making companies would be determined.
A company would be considered loss-making if for every k contiguous days The total income of the company I s negative.
The facepalm owner discussed the situation with its chief accountant, and they decided to the Mpany would look loss-making.
The company report for n days can be represented as a sequence of integers a1, a 2, ..., a n, where ai amthe company income in the dayi (negative values correspond to losses).
The accountant can change any of the values in this sequence, but no updated value can become less than the smallest value in the Original Report-otherwise The updated report would look absolutely implausible. Besides, the accountant wants the total change of the values to be as small as possible.
We'll assume that's the total change of the values are, where is the I-th value in the updated report.
Your task is to calculate the minimum required total change of the values and provide the updated report.
Input
The first line contains integers n and k (1≤ k ≤ n ≤2 105)-the nu Mber of Days in the "the" and the "number of days in the" the definition of loss-making company.
The second line contains n space-separated integers a1, a2, ..., a c8>N (-10000≤ ai ≤10000), where ai is the COM Pany income in Day I.
It is guaranteed, at least one value of ai am negative.
Output
In the first line print, the required minimum total change. In the second, line print, the corresponding updated report. No value in the updated report can is less than the minimum value of the the original report.
If There is multiple solutions, print any of them.
Sample Input
5 4
3-3-1 1 2
Sample Output
1
2-3-1 1 2
HINT
Test instructions
For this sequence of arbitrary k-length intervals, the sum (k) is less than 0, ask you the minimum change is how much, and the modified value must not be less than the minimum value in the original sequence,
Exercises
Greedy, every time from the K interval right to left to modify
Code
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <ctime>5#include <iostream>6#include <algorithm>7#include <Set>8#include <vector>9#include <queue>Ten#include <map> One#include <stack> A #defineINF 1000000007 - #defineMoD 1000000007 - using namespacestd; the typedef __int64 LL; - inline ll read () - { -ll x=0, f=1; + CharCh=GetChar (); - while(ch<'0'|| Ch>'9') + { A if(ch=='-') f=-1; atCh=GetChar (); - } - while(ch>='0'&&ch<='9') - { -x=x*Ten+ch-'0'; -Ch=GetChar (); in } - returnx*F; to } + //******************************************************************* -ll a[200005]; the * intMain () $ {Panax Notoginseng ll N,k; -ll minn=inf; the +scanf"%i64d%i64d",&n,&k); A for(intI=1; i<=n; i++) thescanf"%i64d", &a[i]), minn=min (minn,a[i]); +ll sum=0; - $ for(intI=1; i<k; i++) $ { -sum+=A[i]; - } thell ans=0; - for(intI=k; i<=n; i++)Wuyi { thesum+= (a[i]-a[i-K]); - if(sum>=0) Wu { -ll tmp=sum+1;//printf ("1111"); Aboutans+=sum+1; $ intj=i; - while(tmp>0) - { - A +ll Gg=min (a[j]-minn,tmp); thea[j]-=GG; -tmp-=GG; $j--; the } thesum=-1; the } the } -printf"%i64d\n", ans); in for(intI=1; i<=n; i++) the { theprintf"%i64d", A[i]); About } the return 0; the}
View Code
Codeforces Gym 100513G G. Facepalm Accounting