#P9505. Magic Ring Activation

    ID: 22654 Type: Default 1000ms 256MiB

Magic Ring Activation

Magic Ring Activation

Little M is faced with the task of activating his soul artifact – the Magic Ring. The ring has n nodes arranged in a circle, and on each node there is a magical sprite. Each sprite has a fixed magic offering value. The magic offerings form a permutation of the numbers from \(0\) to \(n-1\).

Little M may choose to activate or not activate any sprite, but in order to activate the ring, at least \(k \ (\ge 2)\) sprites must be activated.

Each sprite contributes an enchantment value regardless of whether it is activated or not. The enchantment rules are as follows:

  • If a sprite is activated, its enchantment value is the square of its magic offering, i.e. \(a^2\) where \(a\) is its magic offering value.
  • If a sprite is not activated, it will look in both directions (clockwise and anticlockwise) along the ring until it finds the closest activated sprite in each direction. Let the distances be \(d_1\) and \(d_2\) and their magic offerings be \(a_1\) and \(a_2\); the non‐activated sprite picks the direction whose activated sprite has the higher magic offering. Its enchantment value is the product of the chosen activated sprite's magic offering and the distance from it. Formally, if the chosen activated sprite has magic offering \(a\) and the distance is \(d\), then its contribution is \(a \times d\).

The objective is to choose which sprites to activate (with at least \(k\) activated) so that the sum of the enchantment values of all sprites is minimized. Compute and output this minimum total enchantment value.

inputFormat

The first line contains two integers \(n\) and \(k\) (\(2 \le k \le n\)), representing the number of nodes and the minimum number of sprites to activate. The second line contains \(n\) space‐separated integers, representing a permutation of \(0, 1, \ldots, n-1\): the magic offering values at nodes \(0\) to \(n-1\) in order (the nodes are arranged in a circle).

outputFormat

Output a single integer: the minimum total enchantment value achievable.

sample

3 2
1 0 2
2