l-talented Chef (Math problem)

Source: Internet
Author: User

Talented Chef Time limit: 2 Seconds Memory Limit: 65536 KB

As we all know, Coach Gao was a talented chef, because he was able to cook M dishes in the same time. Tonight he is going to has a hearty dinner with his girlfriend at his home. Of course,Coach Gao is going-cook all dishes himself, in order-show off his genius cooking skill-his gir Lfriend.

To make full use of the He genius in cooking, Coach Gao decides to prepare dishes for the N dinner. The i -th dish contains Ai steps. The steps of a dish should be finished sequentially. In all minute of the cooking,Coach Gao can choose at most M different dishes and finish one step to each di SH chosen.

Coach Gao wants to know the least time he needs to prepare the dinner.

Input

There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:

The first line contains integers N M and (1 <= N , M <= 40000). The second line contains N integers Ai (1 <= Ai <= 40000).

Output

For each test case, the output of the least time (in minute) to finish all dishes.

Sample Input
23 22 2 210 61 2 3 4 5 6 7 8 9 10
Sample Output

310

It's a thought problem, and the first thing I want to do is sum it up and divide it by M, which is the least number of steps.

But it is not, but also to consider the maximum number of steps it bakes, in fact, it is so many cakes, bake the longest cake needs the number of steps.

While it is said that the minimum number of steps is required, it is necessary to meet two conditions at a time.

So the last thing to ask is the biggest one of the two.

#include <stdio.h> #include <string.h>int max (int a,int b) {return a>b?a:b;} int main () {int t,i,j,n,m,sum,maxn,a;scanf ("%d", &t), while (t--) {maxn=sum=0;scanf ("%d%d", &n,&m); for (I=1 ; i<=n;i++) {scanf ("%d", &a), sum+=a;//to find the larger one, and then save it in MAXN. Maxn=max (A,MAXN);} int ans=0;//means that if sum cannot be divisible by M-step, then one more step is added. if (sum%m) ans=sum/m+1;//if divisible, then the direct sum divided by M is good; else if (sum%m==0) ans=sum/m;//returns two of the larger one; ans=ans>maxn?ans:maxn; printf ("%d\n", ans);}}


l-talented Chef (Math problem)

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.