Bookface
Time limit2sMemory limit512 MB
Given daily line counts, find the fewest single-line edits so that every pair of days differs by at least d, with counts staying non-negative.
Problem
Taking on an ambitious project? Check. Working tirelessly, day and night, to deliver it before the deadline? Check. Committing your code every day in small chunks? Check. After all that work, you, the most ambitious software developer at Bookface to date, thought nothing stood between you and your promotion. You could not have been more wrong.
Before sending your promotion package to management, you asked your coworker Little Franiu to take a look. One quick look was enough to see the problem. "At Bookface, you have to move fast," said Franiu. "Move fast and change things. You can't be predictable and ship similarly sized commits every day like that!"
You looked at the number of lines you added on each of the days of the project, and saw that Franiu had a point. Let be this count for the -th day. All the values are relatively close to each other. Franiu had an idea for fixing this: you can rewrite your commit history to make it look better.
Together with Franiu, you chose a value and decided that must hold for any . To that end, you can select a single day and either add or remove one line of code in the commit made on that day. You can perform arbitrarily many such operations, and each takes 1 second. How much time do you need to reach your goal? The number of lines in a commit must always stay non-negative.
Input
The first line contains the number of test cases (). Each test case starts with a line containing and (, ). The next line contains integers (), the number of lines added on the -th day. The total number of days over all test cases does not exceed .
Output
For each test case, output a single line containing one integer: the number of seconds needed to accomplish your goal.