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 pi. 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?
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 p1, p2, ..., pn (1 ≤ pi ≤ 105), where pi is the price of the i-th game.
Print the maximum number of games Polycarp can get today.
5 5 3 1 5 6
3
2 7 7
2
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.
题目大意:A要买一个游戏光盘,由于做活动,所以买一个价值为w的游戏盘,可以免费获赠总价值小于w的任意游戏组合(个数不限),现求能买到的游戏盘的总个数最大为多少。
解题思路:贪心。最大的是自己肯定买价值最大的盘,然后再选择获赠价值最少的盘,这样买到的总游戏盘数就会是最大的。
AC代码:
#include <cstdio>
#include <algorithm>
using namespace std;
int a[2005];
int main(){
// freopen("in.txt","r",stdin);
int n;
while(scanf("%d", &n)==1){
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
int ans = 1; //加上自己买的一个
int tt = 0;
for(int i=0; i<n-1; i++){ //如果还可以选择
if(tt + a[i] <= a[n-1]) { tt += a[i]; ans++; }
else break;
}
printf("%d\n", ans);
}
return 0;
}