# Nyoj topic 1091 or 01 backpack (two points, oversized 01 backpack)

Source: Internet
Author: User

or the 01 backpack time limit:10000Ms | Memory Limit:228000KB Difficulty:5
Describe

There are n items with a weight and value of WI and VI, from which the total weight of items not exceeding W is selected, and the maximum value of the sum of the values in all selection schemes is obtained.

Input
Multiple sets of test data.
The first row of each set of test data entered N and W, followed by n lines, each line entered two numbers, representing the first item of WI and VI.
1 <= N <=40
1 <= wi <= 10^15
1 <= VI <= 10^15
1 <= W <= 10^15
Output
each group of data outputs a row representing the maximum value of the sum of values in the selection scheme.
Sample input
`4 52 31 23 42 2`
Sample output
`7`
Source
Challenge Programming
Tc_ Li Yuanhai not done yet. , save for the first collection
`#include <iostream> #include <algorithm> #include <cstdio> #define MAX (A, b) a>b?a:b#define INF 10000000000000000using namespace Std;typedef long long ll;const int MAX = 40; LL Weight[max], Value[max];    LL w;pair<ll, ll> ps[1 << (MAX/2)];int n;void slove () {//enum first half int n2 = N/2;     for (int i = 0; i < 1 << n2; i++)//The first half of the enumeration total is 2^ (N/2);        {LL SW = 0, SV = 0; Each result selects a specific value and weight (i.e altogether 2 things, there are altogether four cases, do not choose, choose the first one, choose the second, all selected) for (int j = 0; J < N2; J + +) {if (I &G                T;> J & 1) {SW + = Weight[j];            SV + = Value[j];    }} Ps[i] = Make_pair (SW, SV);//Add to PS array}//to PS sort (PS, PS + (1 << n2));    PS de-weight int m = 1;    for (int i = 1; i < 1 << n2; i++) if (Ps[m-1].second < Ps[i].second) ps[m++] = ps[i]; LL res = 0;//Save the result//enumerate the second half and find the optimal solution for (int i = 0; i < 1 << (N-N2); i++)//The total number of the same enumeration {LL SW = 0, SV = 0; for (int j = 0; J < N-n2; J + +)//And the first half as {if (i >> J & 1) {s                W + = Weight[n2 + j];            SV + = value[n2 + j]; }} if (sw <= W)//Add a judgement to solve the maximum value, only less than the backpack capacity when {LL TV = (Lower_bound (PS, PS + M, Make_pair (         W-SW, INF))-1)->second;//find the first half of the corresponding value res = Max (res, SV + TV); }} printf ("%lld\n", res);}  int main () {while (~scanf ("%d%lld", &n, &w)) {for (int i = 0; i < n; i++) scanf ("%lld        %lld ", &weight[i], &value[i]);    Slove (); } return 0;}`

Nyoj topic 1091 or 01 backpack (two points, oversized 01 backpack)

Related Keywords:

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.