#P3466. [POI 2008] KLO-Building blocks

    ID: 2536 Type: RemoteJudge 1000ms 125MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>2008平衡树POISpecial Judge

[POI 2008] KLO-Building blocks

题目描述

Byteasar loved to play with building blocks as a child.

He used to arrange the blocks into nn columns of random height and then organize them in the following manner:

Byteasar would choose a number kk and try to arrange the blocks in such a way that some kk consecutive columns would be of equal height. Furthermore he always tried to achieve this goal in a minimum number of moves possible, where a single move consists in:

laying one block on top of any column (Byteasar had a huge box with spare blocks, ensuring this move could always be performed), or removing the uppermost block from the top of any column.

However, Byteasar was never quite sure if his sequence of moves was indeed optimal, therefore he has asked you to write a programme that will help him solve the problem.

Task Write a programme that:

reads the number kk along with the initial setup of the blocks from the standard input,determines the optimal solution (shortest possible sequence of moves),writes out the solution to the standard output.

输入格式

In the first line of the standard input there are two integers, nn and kk (1kn100 0001\le k\le n\le 100\ 000), separated by a single space.

Each of the following nn lines contains the height of some column; the line no. i+1i+1 contains the integer 0hi1 000 0000\le h_i\le 1\ 000\ 000 - the height of the ithi^{th} column, ie. the number of blocks it consists of.

输出格式

The optimal solution should be written out to the standard output, ie. such arrangement of blocks that:

contains kk consecutive columns of equal height, can be obtained from the initial setup in a minimum possible number of moves.

The output should consist of n+1n+1 lines, each one containing a single integer.

The number in the first line should be the minimum number of moves needed to get the desired arrangement.

The line no. i+1i+1 (for 1in1\le i\le n) should contain the number hih'_i - the final height of the ithi^{th} column.

If more than one optimal solution exists, write out one chosen arbitrarily.

5 3
3
9
2
3
1
2
3
9
2
2
2

提示

1kn100 0001\le k\le n\le 100\ 0000hi1 000 0000\le h_i\le 1\ 000\ 000