atcoder#ABC252F. [ABC252F] Bread
[ABC252F] Bread
Score : points
Problem Statement
We have a loaf of bread of length , which we will cut and distribute to children. The -th child wants a loaf of length .
Now, Takahashi will repeat the operation below to cut the loaf into lengths for the children.
Choose a loaf of length and an integer between and (inclusive). Cut the loaf into two loaves of length and . This operation incurs a cost of regardless of the value of .
Each child must receive a loaf of length exactly , but it is allowed to leave some loaves undistributed.
Find the minimum cost needed to distribute loaves to all children.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum cost needed to distribute loaves to all children.
5 7
1 2 1 2 1
16
Takahashi can cut the loaf for the children as follows.
- Choose the loaf of length and , cutting it into loaves of length and for a cost of .
- Choose the loaf of length and , cutting it into loaves of length and for a cost of . Give the former to the -st child.
- Choose the loaf of length and , cutting it into two loaves of length for a cost of . Give them to the -rd and -th children.
- Choose the loaf of length and , cutting it into two loaves of length for a cost of . Give them to the -nd and -th children.
This incurs a cost of , which is the minimum possible. There will be no leftover loaves.
3 1000000000000000
1000000000 1000000000 1000000000
1000005000000000
Note that each child must receive a loaf of length exactly .