Twins

本文探讨了一个有趣的算法问题:如何在不公平的情况下合理地分配硬币。故事设定在一个双胞胎家庭中,母亲匆忙离家前留下一些硬币作为午餐费,并请求孩子公平分配。然而,其中一个孩子决定采取策略,挑选最少数量的硬币使得其总价值超过剩余硬币的价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It's hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it's like.

Now let's imagine a typical morning in your family. You haven't woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, ncoins of arbitrary values a1, a2, ..., an. But as Mom was running out of time, she didn't split the coins for you two. So she scribbled a note asking you to split the money equally.

As you woke up, you found Mom's coins and read her note. "But why split the money equally?" — you thought. After all, your twin is sleeping and he won't know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you've decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner.

Input

The first line contains integer n (1 ≤ n ≤ 100) — the number of coins. The second line contains a sequence of n integers a1a2, ..., an (1 ≤ ai ≤ 100) — the coins' values. All numbers are separated with spaces.

Output

In the single line print the single number — the minimum needed number of coins.

Example
Input
2
3 3
Output
2
Input
3
2 1 2
Output
2
Note

In the first sample you will have to take 2 coins (you and your twin have sums equal to 6, 0 correspondingly). If you take 1 coin, you get sums 3, 3. If you take 0 coins, you get sums 0, 6. Those variants do not satisfy you as your sum should be strictly more that your twins' sum.

In the second sample one coin isn't enough for us, too. You can pick coins with values 1, 2 or 2, 2. In any case, the minimum number of coins equals 2.


参考代码:

#include <cstdio>

#include <iostream>

#include <algorithm>

using namespace std;

bool cmp(int x,int y)

{

    return x>y;

}

int main ()

{

    int n;

    while(~scanf("%d",&n))

    {

        int a[105],t=0,i,j=0;

        double sum=0,s1=0;

        for(i=0;i<n;i++)

        {

            scanf("%d",&a[i]);

            sum+=a[i];

        }

        sort(a,a+n,cmp);

        

        while(s1<=(sum/2))

        {

            s1+=a[j];

            t++;

            j++;

        }

        printf("%d\n",t);

    }

    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值