Description
There is a water room in the school, the water room is equipped with a total of M faucet for students to open water, each faucet per second
The quantity of water supply is equal to 1.
Now there are n students ready to pick up water, and their initial water order has been determined. Put these students in water order from 1
To n number, I number of students of water is WI. When the water starts, 1 to m students each occupy a faucet, and at the same time play
Turn on the faucet to connect the water. When one of the students J finished their water supply requirements WJ, the next waiting for water students in line K
Immediately take over the position of J classmate began to pick up water. The process of substitution is instantaneous, without any waste of water. That
J students at the end of the first X seconds to complete the water, then K classmate x+1 seconds immediately began to pick up water. If the current number of water access n ' is less than M,
Then only n ' faucet water supply, other m-n ' faucet closed.
Now give the water intake of n students, according to the above water rules, ask all the students how many seconds to finish the water.
Input
Line 1th 2 integers n and m, separated by a space, respectively, indicating the number of water and faucet.
Line 2nd n integers W1, w2 、......、 Wn, separated by a space between each two integers, WI indicates the number I
The amount of water to learn.
Output
The output is only one row, 1 integers, indicating the total time required to receive water.
Sample Input
6 2
5 4 6 2 1 7
Sample Output
40
After each walk of the sequence, directly on the code
Code
#include <cstdio>
#include <algorithm>
using namespace std;
int main ()
{
int n,m,i,sum=0;
int a[1001],b[1001];
scanf ("%d%d", &n,&m);
for (i=1;i<=n;i++)
scanf ("%d", &a[i]);
Sort (a+1,a+n+1);
for (i=1;i<=m;i++)
b[i]=a[i];
for (i=m;i<=n;i++)
b[i]=b[i-m]+a[i];
for (i=1;i<=n;i++)
sum+=b[i];
printf ("%d", sum);
}