Algorithm Training Boxing problem

Source: Internet
Author: User

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]&GT;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

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.