DMOPC '21 Contest 8 P5 - Tree Building

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

For her final CS project, Mafuyu needs to build a tree with N nodes. Where is she going to get the nodes from? The convenience store, of course! The convenience store offers a wide variety of nodes for various prices. Specifically, for each 1iK, it has an unlimited supply of nodes with i connectors coming out of it for ai yen each.

After buying the required nodes, Mafuyu needs to go home and actually build the tree by repeatedly melding two connectors from two different nodes into a single edge connecting the two nodes. The final product should not have any leftover connectors that are not melded.

Given these conditions, please find the minimum total cost required for Mafuyu to build a tree with N nodes!

Constraints

3N109

2K3×103

K<N

1ai109

Subtask 1 [15%]

N300

Subtask 2 [20%]

N3×103

Subtask 3 [20%]

N2×105

K300

Subtask 4 [20%]

K300

Subtask 5 [25%]

No additional constraints.

Input Specification

The first line contains 2 space-separated integers N and K.

The next line contains K space-separated integers a1,a2,,aK.

Output Specification

Output the minimum total cost (in yen) required for Mafuyu to build a tree with N nodes.

Sample Input

Copy
4 3
1 3 4

Sample Output

Copy
7

Explanation

Mafuyu can buy 3 nodes with 1 connector and 1 node with 3 connectors, forming the following tree:


Comments

There are no comments at the moment.