Codeforces 808E Selling Souvenirs(花费是倍数关系的背包)


E. Selling Souvenirs
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it’s an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market.

This morning, as usual, Petya will come to the market. Petya has n different souvenirs to sell; ith souvenir is characterised by its weight wi and cost ci. Petya knows that he might not be able to carry all the souvenirs to the market. So Petya wants to choose a subset of souvenirs such that its total weight is not greater than m, and total cost is maximum possible.

Help Petya to determine maximum possible total cost.

The first line contains two integers n and m (1 ≤ n ≤ 100000, 1 ≤ m ≤ 300000) — the number of Petya’s souvenirs and total weight that he can carry to the market.

Then n lines follow. ith line contains two integers wi and ci (1 ≤ wi ≤ 3, 1 ≤ ci ≤ 109) — the weight and the cost of ith souvenir.

Print one number — maximum possible total cost of souvenirs that Petya can carry to the market.

1 1
2 1
2 2
1 3
2 2
4 3
3 10
2 7
2 8
1 1






#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
#define LL long long

const int MAXM=300000+3;

struct Statue
    int p1, p2;//花费为1,2的物品的使用个数
    LL val;//当前最大价值

int N, M;
vector<int> save_val[5];

int main()
    scanf("%d%d", &N, &M);
    for(int i=0;i<N;++i)
        int w, v;
        scanf("%d%d", &w, &v);
    for(int i=1;i<=3;++i)
        sort(save_val[i].begin(), save_val[i].end(), greater<int>());
    for(int i=1;i<=M;++i)
        if(dp[i-1].p1+1<=save_val[1].size() && dp[i-1].val+save_val[1][dp[i-1].p1]>dp[i].val)
        if(i>1 && dp[i-2].p2+1<=save_val[2].size() && dp[i-2].val+save_val[2][dp[i-2].p2]>dp[i].val)
    LL sum=0, ans=0;
    for(int i=0;i<=save_val[3].size() && i*3<=M;++i)
        ans=max(ans, sum+dp[M-i*3].val);
    printf("%lld\n", ans);

    return 0;

Buy the souvenirs Buy the souvenirs


Problem DescriptionrnWhen the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will buy some ones with pleasure. Not only can they give the souvenirs to their friends and families as gifts, but also can the souvenirs leave them good recollections. All in all, the prices of souvenirs are not very dear, and the souvenirs are also very lovable and interesting. But the money the people have is under the control. They can’t buy a lot, but only a few. So after they admire all the souvenirs, they decide to buy some ones, and they have many combinations to select, but there are no two ones with the same kind in any combination. Now there is a blank written by the names and prices of the souvenirs, as a top coder all around the world, you should calculate how many selections you have, and any selection owns the most kinds of different souvenirs. For instance:rnrnrnrnrnAnd you have only 7 RMB, this time you can select any combination with 3 kinds of souvenirs at most, so the selections of 3 kinds of souvenirs are ABC (6), ABD (7). But if you have 8 RMB, the selections with the most kinds of souvenirs are ABC (6), ABD (7), ACD (8), and if you have 10 RMB, there is only one selection with the most kinds of souvenirs to you: ABCD (10).rn rnrnInputrnFor the first line, there is a T means the number cases, then T cases follow.rnIn each case, in the first line there are two integer n and m, n is the number of the souvenirs and m is the money you have. The second line contains n integers; each integer describes a kind of souvenir. rnAll the numbers and results are in the range of 32-signed integer, and 0<=m<=500, 0 问答