0-1 Backpack Modified Version

Source: Internet
Author: User

Title Description

0 Kawasaki has a lot of friends, including one named LFJ.

LFJ is a hand residue, he and 0 Qi play online games when not good hit Ben, every day to see the auction house, not long before, became an excellent businessman. But the best speculators have missed the day of being a tray man. The so-called real tray-man never keeps himself alive. When LFJ the success of the tray, that is, there is no day left.

As a friend of LFJ, 0 Kawasaki really can't see down, so he decided to help lfj a. Of course, 0 Kawasaki certainly will not do their own work, and you have to work.

LFJ can give you the price and profit of all the goods you can buy, and the principal of the auction house. Since it is the tray-man, there must be nothing left. While the LFJ wants you to give the most profitable purchase option in the tray, it feels as if you know the maximum profit.

Input

The first behavior for each group of data is two integers p and N, representing the principal and the number of auction items. (Note: There is only one item for each category with Question B.)

Next n lines, two data per line PI,CI represents the profit and purchase price of Class I items.

1<=p<=20000,1<=n<=300,1<=c,p<=200

Output

For each set of data, the output line is the maximum profit that can be obtained

If the tray cannot succeed, the output JPX

Input sample
3 12 14 33 11 32 2
Output sample
jpx4
Hint

Do not call Max () to prevent timeouts by using the IF direct comparison

Title Source: Http://biancheng.love/contest/17/problem/C/index

Problem-Solving Analysis: The question and 0-1 knapsack problems similar, but requires the implementation of the entire capacity of the backpack V, that is, the title of the principal p, all exhausted, and demand the most profit. Each item that is loaded needs to be judged, first, whether it can satisfy the exhaustion of the principal, and if it is more than the last profit.

Consider the key code snippet first:

1          for(intI=0; i<n;i++)2         {3scanf"%d%d",&pi,&ci);4              for(intI=s; J >= Ci; j--)5                 if(v[j-ci]!=-1&&v[j]<v[j-ci]+pi)6v[j]=v[j-ci]+Pi;7}

After understanding the above explanations, the following output and input are only carried out according to the requirements of the topic.

Code implementation:

1#include <bits/stdc++.h>2 #defineMax_size 200103 intV[max_size];4 5 using namespacestd;6 7 intMain ()8 {9     intP,n,pi,ci;Ten      while(~SCANF ("%d%d",&p,&N)) One     { Amemset (v,-1,sizeof(V)); -v[0]=0; -          for(intI=0; i<n;i++) the         { -scanf"%d%d",&pi,&ci); -              for(intI=s; J >= Ci; j--) -                 if(v[j-ci]!=-1&&v[j]<v[j-ci]+pi) +v[j]=v[j-ci]+Pi; -         } +         if(v[p]==-1) Aprintf"jpx\n"); at         Else -printf"%d\n", V[p]); -     } -}

An implementation code with 0-1 backpacks is also included:

1 void Zoreonepack (intint  weight)2{3for      (int i = W; I >= weight; -- i)4         f[i] = max (f[i],f[i-weight]+ cost); 5 }

0-1 Backpack Modified Version

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.