-
-
Title Description:
-
Long time ago, Kitty lived in a small village. The air was fresh and the scenery were very beautiful. The only thing that troubled she is the typhoon.
When the typhoon came, everything is terrible. It kept blowing and raining for a long time. And what made the situation worse is that's all of the Kitty's walls were made of wood.
One day, Kitty found that there is a crack in the wall. The shape of the crack is
A rectangle with the size of 1xL (inch). Luckly Kitty got N blocks and a saw (saw) from her neighbors.
The shape of the blocks were rectangle too, and the width of all blocks were 1 inch. So, with the help of saw, Kitty could cut some of the blocks (of course she could use it directly without cutting) and Put them in the crack, and the wall is repaired perfectly, without any gap.
Now, Kitty knew the size of each blocks, and wanted to use as fewer as possible of the blocks to repair the wall, could yo U help her?
-
Input:
-
The problem contains many test cases, please process to the end of file (EOF).
Each test case contains the lines.
In the first line, there is integers L (0<l<1000000000) and N (0<=n<600) which
mentioned above.
In the second line, there is N positive integers. The ith integer Ai (0<ai<1000000000) means that the ith block have the size of 1xAi (in inch).
-
Output:
-
For each test case, print an integer which represents the minimal number of blocks is needed.
If Kitty could not repair the wall, just print "impossible" instead.
-
Sample input:
-
5 33 2 15 22 1
-
Sample output:
-
2impossible
To use the Arryays.sort method to implement a descending sort of an array of type int, you can only increment the sort first, and then invert it.
1 Importjava.util.Arrays;2 ImportJava.util.Scanner;3 Public classmain{4 Public Static voidMain (String[]args) {5Scanner in=NewScanner (system.in);6 while(In.hasnext ()) {7 intL=in.nextint ();8 intn=in.nextint ();9 int[]a=New int[N];Ten for(inti=0;i<n;i++){ Onea[i]=in.nextint (); A } - Arrays.sort (A); - int[]b=New int[N]; the for(inti=0;i<n;i++){ -B[i]=a[n-i-1]; - } - intCout=0; + intSum=0; - for(inti=0;i<n&&sum<l;i++){ +sum+=B[i]; Acout++; at } - if(sum>=L) { - System.out.println (cout); - } - Else{ -System.out.println ("Impossible"); in } - } to } + } - /************************************************************** the problem:1436 * user:0000h $ Language:javaPanax Notoginseng result:accepted - time:360 Ms the memory:29688 KB + ****************************************************************/
Title 1436:repair the Wall