I - Sale in GameStore Gym - 100513I

A well-known Berland online games store has announced a great sale! Buy any game today, and you can download more games for free! The only constraint is that the total price of the games downloaded for free can’t exceed the price of the bought game.

When Polycarp found out about the sale, he remembered that his friends promised him to cover any single purchase in GameStore. They presented their promise as a gift for Polycarp’s birthday.

There are n games in GameStore, the price of the i-th game is p i. What is the maximum number of games Polycarp can get today, if his friends agree to cover the expenses for any single purchase in GameStore?

Input
The first line of the input contains a single integer number n (1 ≤ n ≤ 2000) — the number of games in GameStore. The second line contains n integer numbers p 1, p 2, …, p n (1 ≤ p i ≤ 105), where p i is the price of the i-th game.

Output
Print the maximum number of games Polycarp can get today.

Examples
Input
5
5 3 1 5 6
Output
3
Input
2
7 7
Output
2
Note
In the first example Polycarp can buy any game of price 5 or 6 and download games of prices 1 and 3 for free. So he can get at most 3 games.

In the second example Polycarp can buy any game and download the other one for free.

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>

using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;

int a[maxn];

int main() {
    int n;scanf("%d",&n);
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
    }
    sort(a + 1,a + 1 + n);
    int mx = a[n];
    int ans = 1;
    for(int i = 1;i < n;i++) {
        if(mx >= a[i]) {
            mx -= a[i];
            ans++;
        }
        else break;
    }
    printf("%d\n",ans);
    return 0;
}
原创文章 930 获赞 38 访问量 5万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/106057926
今日推荐