目录
目录
题目链接
AGC027 A - Candy Distribution Again
题解
贪心即可
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#define gc getchar()
#define pc putchar
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c > '9') {if(c == '-')f = -1; char c = getchar();}
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-');
x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
const int maxn = 100007;
int a[maxn];
int main() {
int n = read(),k = read();
for(int i = 1;i <= n;++ i) a[i] = read();
std::sort(a + 1, a + n + 1);
int ans = 0;
for(int i = 1;i <= n;++ i) {
if(i < n) { if(a[i] <= k ) k -= a[i],ans ++;}
else if(i == n && a[i] == k) ans ++;
else break;
}
print(ans);
pc('\n');
}