Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Hints: Array Splitting

a
b
c
d
e

Consider any random partition and compress all elements in a segment with its sum.

f(i)=a+2b+3c+4d+5e

Express f(i) using suffix sums. Assume Sc denotes suffix sum starting from the segment containing c.

f(i)=Sa+Sb+Sc+Sd+Se

Hence, the cost is just the sum of k elements of the suffix array. How do you maximize it?

Just sort the suffix sum array and pick the maximum k elements from the suffix array. There’s a small catch.
The first suffix sum necessarily needs to be taken. The rest k1 elements can be chosen freely.