NOIP2010 universal group line up Water

Source: Internet
Author: User
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);    
}  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.