A well-known Berland onlinegames store has announced a great sale! Buy any game today, and you candownload more games for free! The only constraint is that the total price ofthe games downloaded for free can't exceed the price of the bought game.
When Polycarp found outabout the sale, he remembered that his friends promised him to cover any singlepurchase in GameStore. They presented their promise as a gift for Polycarp'sbirthday.
There are n gamesin GameStore, the price of the i-th game is pi. What is the maximumnumber of games Polycarp can get today, if his friends agree to cover theexpenses for any single purchase in GameStore?
Input
The first line of the inputcontains a single integer number n (1 ≤ n ≤ 2000) — the number of games in GameStore. The second linecontains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ 105), where pi is the price of the i-th game.
Output
Print the maximum number ofgames Polycarp can get today.
Sample test(s)
input
5
5 3 1 5 6
output
3
input
2
7 7
output
2
Note
In the first examplePolycarp can buy any game of price 5 or 6 and download games of prices 1 and 3for free. So he can get at most 3 games.
In the second examplePolycarp can buy any game and download the other one for free.
思路:
贪心,排序,取最大的数,从小的数开始减
程序:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 2005;
struct node
{
int x, y;
};
int main()
{
int n;
scanf("%d\n", &n);
int i;
int p[N];
for (i = 0; i<n; i++)
{
scanf("%d", &p[i]);
}
sort(p, p + n);
int m = p[n - 1];
int ans = 1;
n--;
for (i = 0; i<n; i++)
if (m >= p[i])
{
m -= p[i];
ans++;
}
printf("%d\n", ans);
return 0;
}

在游戏店举办的促销活动中,玩家只需购买一款游戏即可免费下载更多游戏,总价值不得超过所购游戏价格。主角Polycarp利用此机会,通过朋友的支付承诺,最大化地获取游戏数量。

735

被折叠的 条评论
为什么被折叠?



