现在你用你的钱凑一些数,如果你的钱能凑成[0,x]里面所有的数,那么你将会免费获得该店价值x元的代金券,假设你有四个硬币面值分别为1,2,4,100,你就可以凑成[0,7]里面所有的数,我们将会送你7元的代金券。现在就用你的硬币来试试吧。Enjoy yourself!”
Input
第一行一个整数T,表示数据组数。对于每组数据,首先读入一个整数n(n<=100000),然后接下来的一行有n个整数,表示a[i] (0<a[i]<=1e9)
Output
对于每个数据,输出一个整数x,表示店主要送x元的代金券给TMK
Sample Input
1
3
1 2 3
Sample Output
6
思路:首先对数组进行排序,假定对于前 i 个,都能够组成0-y+1;
那么当 a[i] > y+1 时,说明不能形成
否则可以继续,此时的 y 应加上 a[i];
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int a[110000];
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i=0; i<n; i++)
cin >> a[i];
sort(a, a+n);
long long y = 0;
for(int i=0; i<n; i++)
{
if(a[i] > y+1)
break;
else if(a[i] <= y+1)
y += a[i];
}
cout << y << endl;
}
return 0;
}