CF1675G.Sorting Pancakes
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Nastya baked m pancakes and spread them on n dishes. The dishes are in a row and numbered from left to right. She put ai pancakes on the dish with the index i .
Seeing the dishes, Vlad decided to bring order to the stacks and move some pancakes. In one move, he can shift one pancake from any dish to the closest one, that is, select the dish i ( ai>0 ) and do one of the following:
- if i>1 , put the pancake on a dish with the previous index, after this move ai=ai−1 and ai−1=ai−1+1 ;
- if i<n , put the pancake on a dish with the following index, after this move ai=ai−1 and ai+1=ai+1+1 .
Vlad wants to make the array a non-increasing, after moving as few pancakes as possible. Help him find the minimum number of moves needed for this.
The array a=[a1,a2,…,an] is called non-increasing if ai≥ai+1 for all i from 1 to n−1 .
输入格式
The first line of the input contains two numbers n and m ( 1≤n,m≤250 ) — the number of dishes and the number of pancakes, respectively.
The second line contains n numbers ai ( 0≤ai≤m ), the sum of all ai is equal to m .
输出格式
Print a single number: the minimum number of moves required to make the array a non-increasing.
输入输出样例
输入#1
6 19 5 3 2 3 3 3
输出#1
2
输入#2
3 6 3 2 1
输出#2
0
输入#3
3 6 2 1 3
输出#3
1
输入#4
6 19 3 4 3 3 5 1
输出#4
3
输入#5
10 1 0 0 0 0 0 0 0 0 0 1
输出#5
9
说明/提示
In the first example, you first need to move the pancake from the dish 1 , after which a=[4,4,2,3,3,3] . After that, you need to move the pancake from the dish 2 to the dish 3 and the array will become non-growing a=[4,3,3,3,3,3] .