Under construction

Still being built. Things may change

Partitioning a Sequence by Score

Time limit8sMemory limit1024 MB

Summary
Split the sequence into contiguous parts and find the k-th largest total of the squared part sums over all partitions.
Level

Medium7 of 10

Topics
Dynamic programming, Prefix sum, Heap
Solved
No attempts yet

Problem

Given an integer sequence A=(a1,…,an)A = (a_1, \ldots, a_n) of length nn, call (B1,…,Bm)(B_1, \ldots, B_m) a partition of AA (m≥1m \ge 1) when both of the following hold.

  • Each BiB_i (1≤i≤m1 \le i \le m) is an integer sequence of length at least 1.
  • Concatenating B1,…,BmB_1, \ldots, B_m in this order gives AA.

For example, (3,1,4,1,5)(3, 1, 4, 1, 5) and ((3),(1,4),(1,5))((3), (1, 4), (1, 5)) are both partitions of (3,1,4,1,5)(3, 1, 4, 1, 5).

For an integer sequence BB, let f(B)f(B) be the square of the sum of the elements of BB. For a partition (B1,…,Bm)(B_1, \ldots, B_m) of AA, the score of the partition is f(B1)+⋯+f(Bm)f(B_1) + \cdots + f(B_m). For example, the score of ((3),(1,4),(1,5))((3), (1, 4), (1, 5)) is 32+(1+4)2+(1+5)2=703^2 + (1+4)^2 + (1+5)^2 = 70.

AA has exactly 2n−12^{n-1} partitions. Sort the scores of all partitions of AA in descending order and find the kk-th value.

Input

The input consists of multiple datasets. The number of datasets does not exceed 30. Each dataset is given in the following format.

nn kk

a1a_1 a2a_2 …\ldots ana_n

nn and kk are integers with 1≤n≤10001 \le n \le 1000 and 1≤k≤min⁡{2n−1,2000}1 \le k \le \min\{2^{n-1}, 2000\}. Each aia_i is the ii-th element of AA, an integer with −106≤ai≤106-10^6 \le a_i \le 10^6.

The input ends with a line containing two zeros.

Output

For each dataset, print on one line the kk-th largest score among all partitions of AA.

Examples1

  1. Example 1

    Input
    5 1
    3 1 4 1 5
    5 6
    3 1 4 1 5
    14 255
    2024 6 29 14 0 -17 0 2024 7 5 16 30 -19 30
    0 0
    
    Expected output
    196
    100
    8484005