1. 程式人生 > >codeforces round#522 E - The Unbearable Lightness of Weights

codeforces round#522 E - The Unbearable Lightness of Weights

You have a set of nn weights. You know that their masses are a1a1, a2a2, ..., 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 kkweights with the total mass mm (both parameters kk 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 nn (1≤n≤1001≤n≤100) — the number of weights.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤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.

Examples

Input

4
1 4 2 2

Output

2

Input

6
1 2 4 4 4 9

Output

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}{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}{1,4}. It is easy to see that the two remaining weights have mass of 22 grams each.

In the second example we can ask for a subset of two weights with total mass being 88, and the only answer is {4,4}{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個砝碼,你不知道每個砝碼的重量,但是你的朋友知道,你可以每次拿出k個砝碼給你的朋友,他會告訴你k個砝碼的重量和,經過多次嘗試之後,你最多可以區分出多少個砝碼?

題解:首先對於你拿出的k個砝碼,若要想區分出來,這k個砝碼必然是相同的且其質量和的方案數唯一,那麼對於所有砝碼的重量和的方案數可以用多重揹包來求解,最後特判只有2種物品的就可以了。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+7;

int a[maxn],dp[maxn][105];
int main()
{
    int n;
    scanf("%d",&n);
    map<int,int>m;

    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        m[ a[i] ]++;
    }

    vector<pair<int,int> >v;

    for(auto p:m) v.push_back(p);

    dp[0][0]=1;
    for(auto p:v)
    {
        int vol=p.first;
        int k=p.second;
        for(int i=1e4;i>0;i--)
            for(int j=1;j<=n;j++)
                for(int t=1;t<=k && t<=j && i-vol*t>=0;t++)
                {
                    dp[i][j]+=dp[i-vol*t][j-t];
                    dp[i][j]=min(dp[i][j],2);
                }
    }

    int ans=1;
    for(auto p:v)
    {
        int vol=p.first;
        int k=p.second;
        for(int i=1;i<=k;i++)
            if(dp[i*vol][i]==1)
            {
                if(v.size()==2 && i==k)
                {
                    printf("%d\n",n);
                    return 0;
                }
                else ans=max(ans,i);
            }
    }

    printf("%d\n",ans);
    return 0;
}