"DP Basics" 01 Backpack-Review

Source: Internet
Author: User

1066:0/1 Backpack time limit: 1 Sec Memory Limit: MB
Title Description

A traveler has a backpack with a maximum of M kg, and now has n items, the weights of which are w1,w2,...,wn respectively, and their value is C1,C2,..., Cn. If there is only one piece of each item, the traveler can get the maximum total value.

Input

First line: Two integers, M (backpack capacity, m<=200) and N (number of items, n<=30);
2nd.. N+1 line: Two integer wi,ci per line, indicating the weight and value of each item.

Output

A single row, a number, representing the maximum total value.

Sample input
10 42 13 34 57 9
Sample output
12

Exercises

is 0/1 backpack, but input comparison egg pain, first input m and then enter N.

Review the DP equation for the 0/1 backpack: F[i]=max (F[j-c[i]]+w[i]) J from M Downto C[i].

1#include <bits/stdc++.h>2 using namespacestd;3 intf[700010],n,m,c[510],w[510];4 intMain () {5scanf"%d%d",&m,&n);6      for(intI=1; i<=n;++i) scanf ("%d%d",&c[i],&W[i]); 7      for(intI=0; i<=m;++i) f[i]=0;8      for(intI=1; i<=n;++i)9          for(intj=m;j>=c[i];--j)TenF[j]=max (f[j],f[j-c[i]]+w[i]); Oneprintf"%d\n", F[m]); A     return 0; -}

"DP Basics" 01 Backpack-Review

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.