## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

## Sorting Pancakes Codeforces Solution | Codeforces Problem Solution 2022

G. Sorting Pancakes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nastya baked $m$ pancakes and spread them on $n$ dishes. The dishes are in a row and numbered from left to right. She put ${a}_{i}$ 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$ (${a}_{i}>0$) and do one of the following:

• if $i>1$, put the pancake on a dish with the previous index, after this move ${a}_{i}={a}_{i}-1$ and ${a}_{i-1}={a}_{i-1}+1$;
• if $i, put the pancake on a dish with the following index, after this move ${a}_{i}={a}_{i}-1$ and ${a}_{i+1}={a}_{i+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=\left[{a}_{1},{a}_{2},\dots ,{a}_{n}\right]$ is called non-increasing if ${a}_{i}\ge {a}_{i+1}$ for all $i$ from $1$ to $n-1$.

Input

The first line of the input contains two numbers $n$ and $m$ ($1\le n,m\le 250$) — the number of dishes and the number of pancakes, respectively.

The second line contains $n$ numbers ${a}_{i}$ ($0\le {a}_{i}\le m$), the sum of all ${a}_{i}$ is equal to $m$.

Output

Print a single number: the minimum number of moves required to make the array $a$ non-increasing.