GUPTA MECHANICAL

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

Thursday, 5 May 2022

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 ai pancakes on the dish with the index i.

Solution Click Below:- CLICK HERE

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=ai1 and ai1=ai1+1;
  • if i<n, put the pancake on a dish with the following index, after this move ai=ai1 and ai+1=ai+1+1.

Vlad wants to make the array anon-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 aiai+1 for all i from 1 to n1.

Input

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

The second line contains n numbers ai (0aim), the sum of all ai is equal to m.

Output

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

 

Join Now for Solution:- 

No comments:

Post a Comment