102525: [AtCoder]ABC252 F - Bread
Description
Score : $500$ points
Problem Statement
We have a loaf of bread of length $L$, which we will cut and distribute to $N$ children.
The $i$-th child $(1\leq i\leq N)$ wants a loaf of length $A_i$.
Now, Takahashi will repeat the operation below to cut the loaf into lengths $A_1, A_2, \ldots, A_N$ for the children.
Choose a loaf of length $k$ and an integer $x$ between $1$ and $k-1$ (inclusive). Cut the loaf into two loaves of length $x$ and $k-x$.
This operation incurs a cost of $k$ regardless of the value of $x$.
Each child $i$ must receive a loaf of length exactly $A_i$, but it is allowed to leave some loaves undistributed.
Find the minimum cost needed to distribute loaves to all children.
Constraints
- $2 \leq N \leq 2\times 10^5$
- $1\leq A_i\leq 10^9$
- $A_1+A_2+\cdots+A_N\leq L\leq 10^{15}$
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $L$ $A_1$ $A_2$ $\ldots$ $A_N$
Output
Print the minimum cost needed to distribute loaves to all children.
Sample Input 1
5 7 1 2 1 2 1
Sample Output 1
16
Takahashi can cut the loaf for the children as follows.
- Choose the loaf of length $7$ and $x=3$, cutting it into loaves of length $3$ and $4$ for a cost of $7$.
- Choose the loaf of length $3$ and $x=1$, cutting it into loaves of length $1$ and $2$ for a cost of $3$. Give the former to the $1$-st child.
- Choose the loaf of length $2$ and $x=1$, cutting it into two loaves of length $1$ for a cost of $2$. Give them to the $3$-rd and $5$-th children.
- Choose the loaf of length $4$ and $x=2$, cutting it into two loaves of length $2$ for a cost of $4$. Give them to the $2$-nd and $4$-th children.
This incurs a cost of $7+3+2+4=16$, which is the minimum possible. There will be no leftover loaves.
Sample Input 2
3 1000000000000000 1000000000 1000000000 1000000000
Sample Output 2
1000005000000000
Note that each child $i$ must receive a loaf of length exactly $A_i$.
Input
题意翻译
你有一根长度为 $L$ 的面包,现在你要将它分给 $N$ 个孩子,第 $i$ 个孩子想要一根长度为 $A_i$ 的面包。 对于一根长度为 $k$ 的面包,你可以选择一个在 $1 \sim k - 1$ 的整数 $x$,将面包切分成长度为 $x$ 和 $k - x$ 的两部分,这将花费 $x$ 的代价。 第 $i$ 个孩子获得的面包长度必须为 $A_i$,但我们允许有面包剩余。 请你花费最少的代价,将这根面包分给孩子们。Output
问题描述
我们有一个长度为$L$的面包,要将其切开并分发给$N$个孩子。
第$i$个孩子($1\leq i\leq N$)想要一个长度为$A_i$的面包。
现在,高桥将重复以下操作,将面包切成长度为$A_1, A_2, \ldots, A_N$的面包分发给孩子们。
选择长度为$k$的面包和一个介于$1$和$k-1$(包括)之间的整数$x$。将面包切成长度为$x$和$k-x$的两个面包。
无论$x$的值如何,此操作的成本为$k$。
每个孩子$i$必须收到长度恰好为$A_i$的面包,但允许留下一些面包不分发。
找出将面包分发给所有孩子的最小成本。
约束
- $2 \leq N \leq 2\times 10^5$
- $1\leq A_i\leq 10^9$
- $A_1+A_2+\cdots+A_N\leq L\leq 10^{15}$
- 输入中的所有值都是整数。
输入
输入从标准输入以以下格式给出:
$N$ $L$ $A_1$ $A_2$ $\ldots$ $A_N$
输出
打印将面包分发给所有孩子的最小成本。
样例输入1
5 7 1 2 1 2 1
样例输出1
16
高桥可以按照以下方式将面包分发给孩子们。
- 选择长度为$7$的面包和$x=3$,将其切成长度为$3$和$4$的面包,成本为$7$。
- 选择长度为$3$的面包和$x=1$,将其切成长度为$1$和$2$的面包,成本为$3$。将前者分发给第$1$个孩子。
- 选择长度为$2$的面包和$x=1$,将其切成两个长度为$1$的面包,成本为$2$。将它们分发给第$3$个和第$5$个孩子。
- 选择长度为$4$的面包和$x=2$,将其切成两个长度为$2$的面包,成本为$4$。将它们分发给第$2$个和第$4$个孩子。
这将导致成本为$7+3+2+4=16$,这是可能的最小成本。不会有剩下的面包。
样例输入2
3 1000000000000000 1000000000 1000000000 1000000000
样例输出2
1000005000000000
请注意,每个孩子$i$必须收到长度恰好为$A_i$的面包。