1161: 接水问题

文件提交:无需freopen 内存限制:128 MB 时间限制:1.000 S
评测方式:普通裁判 命题人:
提交:1 解决:1

题目描述

学校里有一个水房,水房里一共装有 lns="http://www.w3.org/1998/Math/MathML"> 个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为 lns="http://www.w3.org/1998/Math/MathML">1

现在有 lns="http://www.w3.org/1998/Math/MathML"> 名同学准备接水,他们的初始接水顺序已经确定。将这些同学按接水顺序从 lns="http://www.w3.org/1998/Math/MathML">1 到 lns="http://www.w3.org/1998/Math/MathML"> 编号,lns="http://www.w3.org/1998/Math/MathML"> 号同学的接水量为 lns="http://www.w3.org/1998/Math/MathML">。接水开始时,lns="http://www.w3.org/1998/Math/MathML">1 到 lns="http://www.w3.org/1998/Math/MathML"> 号同学各占一个水龙头,并同时打开水龙头接水。当其中某名同学 lns="http://www.w3.org/1998/Math/MathML"> 完成其接水量要求 lns="http://www.w3.org/1998/Math/MathML"> 后,下一名排队等候接水的同学 lns="http://www.w3.org/1998/Math/MathML"> 马上接替 lns="http://www.w3.org/1998/Math/MathML"> 同学的位置开始接水。这个换人的过程是瞬间完成的,且没有任何水的浪费。即 lns="http://www.w3.org/1998/Math/MathML"> 同学第 lns="http://www.w3.org/1998/Math/MathML"> 秒结束时完成接水,则 lns="http://www.w3.org/1998/Math/MathML"> 同学第 lns="http://www.w3.org/1998/Math/MathML">+1 秒立刻开始接水。若当前接水人数 lns="http://www.w3.org/1998/Math/MathML"> 不足 lns="http://www.w3.org/1998/Math/MathML">,则只有 lns="http://www.w3.org/1998/Math/MathML"> 个龙头供水,其它 lns="http://www.w3.org/1998/Math/MathML"> 个龙头关闭。

现在给出 lns="http://www.w3.org/1998/Math/MathML"> 名同学的接水量,按照上述接水规则,问所有同学都接完水需要多少秒。

输入

第一行两个整数 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML">,用一个空格隔开,分别表示接水人数和龙头个数。

第二行 lns="http://www.w3.org/1998/Math/MathML"> 个整数 lns="http://www.w3.org/1998/Math/MathML">1,2,,,每两个整数之间用一个空格隔开,lns="http://www.w3.org/1998/Math/MathML"> 表示 lns="http://www.w3.org/1998/Math/MathML"> 号同学的接水量。

输出

一个整数,表示接水所需的总时间。

样例输入

5 3
4 4 1 2 1

样例输出

4

提示

第 lns="http://www.w3.org/1998/Math/MathML">1 秒,lns="http://www.w3.org/1998/Math/MathML">3 人接水。第 lns="http://www.w3.org/1998/Math/MathML">1 秒结束时,lns="http://www.w3.org/1998/Math/MathML">1,2,3 号同学每人的已接水量为 lns="http://www.w3.org/1998/Math/MathML">1,3 号同学接完水,lns="http://www.w3.org/1998/Math/MathML">4 号同学接替 lns="http://www.w3.org/1998/Math/MathML">3 号同学开始接水。

第 lns="http://www.w3.org/1998/Math/MathML">2 秒,lns="http://www.w3.org/1998/Math/MathML">3 人接水。第 lns="http://www.w3.org/1998/Math/MathML">2 秒结束时,lns="http://www.w3.org/1998/Math/MathML">1,2 号同学每人的已接水量为 lns="http://www.w3.org/1998/Math/MathML">2,4 号同学的已接水量为 lns="http://www.w3.org/1998/Math/MathML">1

第 lns="http://www.w3.org/1998/Math/MathML">3 秒,lns="http://www.w3.org/1998/Math/MathML">3 人接水。第 lns="http://www.w3.org/1998/Math/MathML">3 秒结束时,lns="http://www.w3.org/1998/Math/MathML">1,2 号同学每人的已接水量为 lns="http://www.w3.org/1998/Math/MathML">3,4 号同学的已接水量为 lns="http://www.w3.org/1998/Math/MathML">2lns="http://www.w3.org/1998/Math/MathML">4 号同学接完水,lns="http://www.w3.org/1998/Math/MathML">5 号同学接替 lns="http://www.w3.org/1998/Math/MathML">4 号同学开始接水。

第 lns="http://www.w3.org/1998/Math/MathML">4 秒,lns="http://www.w3.org/1998/Math/MathML">3 人接水。第 lns="http://www.w3.org/1998/Math/MathML">4 秒结束时,lns="http://www.w3.org/1998/Math/MathML">1,2 号同学每人的已接水量为 lns="http://www.w3.org/1998/Math/MathML">4,5 号同学的已接水量为 lns="http://www.w3.org/1998/Math/MathML">1lns="http://www.w3.org/1998/Math/MathML">1,2,5 号同学接完水,即所有人完成接水的总接水时间为 lns="http://www.w3.org/1998/Math/MathML">4 秒。