P17078 Summer Desserts。
Background
Starlight Café is about to launch a new season of dessert menu.
To prepare this menu, Natsume Shiki has been making many dessert prototypes in a row, and recorded a corresponding flavor value for each one. However, there are simply too many desserts. If they are all put on the menu just in the order they were made, it will inevitably look messy and will not fully show the features of each dessert.
And there is not much time left before the menu update.
Looking at the neatly arranged prototypes on the table, Natsume decides to reorganize the entire menu so that these desserts can get the highest possible total rating.
Description
The dessert prototypes on the table are arranged in the order they were made, with a total of $n$ items. The flavor value of the $i$-th dessert is a non-negative integer $a_i$.
Natsume plans to partition these $n$ desserts into exactly $k$ groups while keeping the original order. Each group must consist of several consecutive desserts, and each dessert belongs to exactly one group.
For a group that contains desserts from the $l$-th to the $r$-th, Natsume will choose one dessert as the featured dessert of this group. If the $p$-th dessert is chosen as the featured dessert ($l\le p\le r$), then the rating of this group is:
$$
(r-l+1)a_p-\sum_{i=l}^{r}|a_i-a_p|
$$
That is, for a fixed $p$, the rating is $(r-l+1)\times a_p$ minus the sum of absolute differences between $a_i$ and $a_p$ for all $l\le i \le r$ (i.e. the sum of $|a_i-a_p|$ for $l\le i \le r$). We denote this value by $w_p$.
Natsume will choose the dessert that gives the highest rating as the featured dessert for this group. Therefore, define the maximum rating of the interval $[l,r]$ as:
$$
f(l,r)=\max_{l\le p\le r}\left((r-l+1)a_p-\sum_{i=l}^{r}|a_i-a_p|\right)
$$
That is, the maximum among all $w_p$ where the index satisfies $l\le p \le r$.
Suppose the $k$ groups in the final partition correspond to the following intervals in order:
$$
[l_1,r_1],[l_2,r_2],\dots,[l_k,r_k]
$$
These intervals must satisfy:
$$
l_1=1,\quad r_k=n,\quad r_i+1=l_{i+1}\quad(1\le i
Input Format
The first line contains two positive integers $n, k$, representing the length of the sequence and the number of segments.
The second line contains $n$ non-negative integers $a_1, a_2, ... , a_n$, representing the non-negative integer sequence $a$.
Output Format
Output one line with one number, representing the answer.
Explanation/Hint
### Time and Memory Limits
Time limit: $1.0\,\text{s}$.
Memory limit: $512\,\text{MiB}$.
### Constraints
**This problem uses bundled testcases**.
::cute-table{tuack}
| Subtask | $n \le$ | Score |
|:-:|:-:|:-:|
| $1$ | $50$ | $20$ |
| $2$ | $500$ | $20$ |
| $3$ | $5 \times 10^3$ | $20$ |
| $4$ | $10^5$ | $40$ |
For all data, it is guaranteed that $0 \le a_i \le 10^9$, $1 \le k \le n$.
Note: Test points $1\sim4$ are hack data for the four subtasks, respectively. Among the remaining test points, test points $5\sim8$ belong to Subtask $1$, test points $9\sim12$ belong to Subtask $2$, test points $13\sim16$ belong to Subtask $3$, and test points $17\sim24$ belong to Subtask $4$.
### Special Thanks
Idea - Na1L0n9。
Translated by ChatGPT 5