NYOJ289 Apple "01 Backpack"

Source: Internet
Author: User

Apple time limit: theMs | Memory Limit:65535KB Difficulty:3
Describe

CTest has n apples to put into a backpack with a capacity of V. Give the size and price of the first Apple, and find out the total price of the apples that can be put into the backpack.


Input
There are several sets of test data, the first behavior of each group of test data 2 positive integers, respectively, the number of apples is n and the capacity of the backpack V,n, v at the same time to end the test at 0 o'clock, not output. The next n lines, 2 positive integers per line, are separated by a space, representing the size C and the price W of the apples, respectively. The range of all input numbers is greater than or equal to 0, less than or equal to 1000.
Output
output An integer for each set of test data, representing the total value of the apple that can be put into the backpack.
Sample input
3 31 12 13 10 0
Sample output
2
Source
The
Classic problem of dynamic programming

#include <stdio.h> #include <string.h> #define MAXN 1010int dp[maxn];int max (int a, int b) {return a > B? a : b;}  int main () {int V, n, c, W, I, J;while (scanf ("%d%d", &n, &v), n | v) {memset (DP, 0, sizeof (int) * (v + 1)); for (i = 0; I < n; ++i) {scanf ("%d%d", &c, &w); for (j = v; j >= C;--j) dp[j] = max (Dp[j], Dp[j-c] + W);} printf ("%d\n", Dp[v]);} return 0;}


NYOJ289 Apple "01 Backpack"

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.