codeforces round#522-E-The Unbearable Lightness of Weights-----多重背包

You have a set of nn weights. You know that their masses are a1, a2, ..., anan grams, but you don't know which of them has which mass. You can't distinguish the weights.

However, your friend does know the mass of each weight. You can ask your friend to give you exactly k weights with the total mass m (both parameters k and mm are chosen by you), and your friend will point to any valid subset of weights, if it is possible.

You are allowed to make this query only once. Find the maximum possible number of weights you can reveal after this query.

Input

The first line contains a single integer n (1≤n≤1001≤n≤100) — the number of weights.

The second line contains nn integers na1,a2,…,an (1≤ai≤100) — the masses of the weights.

Output

Print the maximum number of weights you can learn the masses for after making a single query.

Input

4
1 4 2 2
2
Input

6
1 2 4 4 4 9
2
Note

In the first example we can ask for a subset of two weights with total mass being equal to 44, and the only option is to get {2,2}.

Another way to obtain the same result is to ask for a subset of two weights with the total mass of 55 and get {1,4}. It is easy to see that the two remaining weights have mass of 2 grams each.

In the second example we can ask for a subset of two weights with total mass being 8, and the only answer is {4,4}. We can prove it is not possible to learn masses for three weights in one query, but we won't put the proof here.

题意:

给你一个n表示物品的数量,然后跟着n个物品的重量,有一个k表示每次可以知道k个物品的重量的和,问你最大可以知道几个物品确切的重量

 

 

 

复习了一波多重背包,注意特判2种 的情况

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+7;
int x,dp[maxn][105],n;
vector<pair<int,int> > vec;
map<int,int> vis;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        vis[x]++;
    }
    for(auto p:vis) vec.push_back(p);/// pair <x,x.cnt>
    if(vec.size()==2) ///特判只有两种质量物品
        return 0*printf("%d\n",n);
    dp[0][0]=1;
    for(auto p:vec){
        int vol=p.first;///物体的权-->质量
        int k=p.second;///个数
        for(int i=1e4;i;i--)///背包容积--->质量
            for(int j=1;j<=n;j++)///放j件时
                for(int t=1; (t<=k && t<=j && i-vol*t>=0) ;t++)///更新方案数,放j件是从第i-1个物体转移过来
                    dp[i][j] += dp[i-vol*t][j-t];
    }
    int ans=1;
    for(auto p:vec){
        int vol = p.first;
        int k = p.second;
        for(int i=1;i<=k;i++)
            if(dp[i*vol][i]==1)///方案唯一则可以判断
                ans=max(ans,i);
    }
    printf("%d\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值