algorithm Training Boxing problemtime limit: 1.0s memory limit: 256.0MB
Problem Description
There is a box with a volume of V (positive integer, 0<=v<=20000), and n items (0
requires n items, any number of boxes in the box, so that the remaining space of the box is minimal. Input FormatThe first behavior is an integer representing the box capacity;
The second act is an integer that indicates that there are n items;
next n rows, an integer per line represents the respective volume of the N items. output FormatAn integer that represents the remaining space of the box.
Sample input
-
6
8
3
A
7
9
7Sample Output0
ImportJava.util.Scanner; Public classMain {Static intv; Static intN; Static intva[]; Static intdp[][]; Static voidsolve () { for(inti=0;i<n;i++){ for(intj=0;j<=v;j++){ if(VA[I]>J) dp[i+1][j]=Dp[i][j]; ElseDp[i+1][j]=math.max (dp[i][j],dp[i][j-va[i]]+Va[i]); }} System.out.println (v-Dp[n][v]); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner sc=NewScanner (system.in); V=Sc.nextint (); N=Sc.nextint (); VA=New int[n]; DP=New int[N+1] [V+1]; for(inti=0;i<n;i++) {Va[i]=Sc.nextint (); } solve (); }}
Algorithm Training Boxing problem