#P9505. 『MGOI』Simple Round I | D. 魔法环

『MGOI』Simple Round I | D. 魔法环

Background

The best weapon is not always the most suitable one; only the most suitable weapon is the best. — Hall Magician W

Problem Description

Little M is facing the task of awakening their soul artifact, the magic ring.

There are nn nodes on the magic ring. Each node has a magic sprite, and each magic sprite has a fixed magic supply value. These magic supply values form a permutation of 0n10 \sim n-1.

Little M may choose to activate or not activate a magic sprite. However, to awaken the magic ring, they must activate at least k(2)k(\ge 2) magic sprites.

Each magic sprite produces an enchantment value regardless of whether it is activated:

  • For an activated magic sprite, the enchantment value it produces is the square of its magic supply value.
  • For a non-activated magic sprite, it looks left and right along the ring, respectively toward the nearest activated magic sprite on each side. It chooses the one with the larger magic supply value as the "target sprite", and the enchantment value it produces equals the product of the magic supply value of this "target sprite" and the distance that the line of sight passes through when looking at this "target sprite".

As a beginner, Little M wants, under the condition of activating the magic ring, to minimize the sum of the enchantment values of all magic sprites, so as to better control the energy of the magic ring.

Input Format

The first line contains two integers n,kn, k.

The second line contains nn integers, representing the magic supply value of the magic sprite on each node.

Output Format

Output one integer in one line, representing the minimum possible sum of enchantment values.

5 2
3 0 1 4 2
7
10 3
2 0 1 5 8 3 4 9 6 7
53

Hint

[Sample 1 Explanation]

Activate the magic sprites with magic supply values 00 and 11.

  • The magic sprite with magic supply value 33 chooses the magic sprite with magic supply value 11, producing an enchantment value of 1×3=31 \times 3 = 3.
  • The magic sprite with magic supply value 00 is activated, producing an enchantment value of 02=00^2=0.
  • The magic sprite with magic supply value 11 is activated, producing an enchantment value of 12=11^2=1.
  • The magic sprite with magic supply value 44 chooses the magic sprite with magic supply value 11, producing an enchantment value of 1×1=11 \times 1 = 1.
  • The magic sprite with magic supply value 22 chooses the magic sprite with magic supply value 11, producing an enchantment value of 1×2=21 \times 2 = 2.

The total enchantment value produced is 77.

[Constraints]

This problem uses bundled Subtask testdata.

For all testdata, 2kn30002\le k \le n \le 3000, k100k \le 100. The magic supply values on each node form a permutation of 0n10\sim n-1.

Subtask nn kk\le Score
11 1010 1313
22 100100 100100 1717
33 300300 2121
44 10001000 2323
55 30003000 2626

Translated by ChatGPT 5