PRATA  Roti Prata
IEEE is having its AGM next week and the president wants to serve cheese prata after the meeting. The subcommittee members are asked to go to food connection and get P(P<=1000) pratas packed for the function. The stall has L cooks(L<=50) and each cook has a rank R(1<=R<=8). A cook with a rank R can cook 1 prata in the first R minutes 1 more prata in the next 2R minutes, 1 more prata in 3R minutes and so on(he can only cook a complete prata) ( For example if a cook is ranked 2.. he will cook one prata in 2 minutes one more prata in the next 4 mins an one more in the next 6 minutes hence in total 12 minutes he cooks 3 pratas in 13 minutes also he can cook only 3 pratas as he does not have enough time for the 4th prata). The webmaster wants to know the minimum time to get the order done. Please write a program to help him out.
Input
The first line tells the number of test cases. Each test case consist of 2 lines. In the first line of the test case we have P the number of prata ordered. In the next line the first integer denotes the number of cooks L and L integers follow in the same line each denoting the rank of a cook.
Output
Print an integer which tells the number of minutes needed to get the order done.
Example
Input:
3
10
4 1 2 3 4
8
1 1
8
8 1 1 1 1 1 1 1 1
Output:
12
36
1
hide comments
amit125992:
20181229 11:22:08
Last edit: 20181229 11:31:47 

yaseenmollik:
20181111 18:26:25
Had to learn Min Heap and Priority Queue just to solve this. And finally ac :) Last edit: 20181111 18:26:43 

rajat_123:
20181101 11:55:55
Set upper limit as 10000007 in Java, it gives TLE with 10^9 :) 

ompr_07:
20181004 19:22:49
Binary Search AC in one go!! 

codeinfinity:
20181002 20:16:28
I agree with @pallindromeguy


hvr81070:
20180903 21:10:11
#40 :) 

nadstratosfer:
20180316 18:20:48
shadow10, the cooks' finishing time for each prata:


pallindromeguy:
20180316 09:50:15
Take upper limit to be 10000007 ;) 

praney_rai:
20170616 11:58:30
Binary Search Zinda Bad :P (Got WA using cin , cout but AC with fast i/o without changing even single line )WTF.. 

shadow10:
20170316 16:25:13
I can't understand the first test case.I guess time should be greater than 12 min or i am mistaking somewhere. 
Added by:  Saransh Bansal 
Date:  20110514 
Time limit:  0.125s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ASM64 
Resource:  Own problem NTU IEEE codejam 2011 