Partitioning a Sequence by Score
Time limit8sMemory limit1024 MB
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 of length , call a partition of () when both of the following hold.
- Each () is an integer sequence of length at least 1.
- Concatenating in this order gives .
For example, and are both partitions of .
For an integer sequence , let be the square of the sum of the elements of . For a partition of , the score of the partition is . For example, the score of is .
has exactly partitions. Sort the scores of all partitions of in descending order and find the -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.
and are integers with and . Each is the -th element of , an integer with .
The input ends with a line containing two zeros.
Output
For each dataset, print on one line the -th largest score among all partitions of .