计蒜客 39268.Tasks (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest A.) 2019ICPC西安邀请赛现场赛重现赛

Tasks

It's too late now, but you still have too much work to do. There are nn tasks on your list. The ii-th task costs you t_itiseconds. You want to go to bed TT seconds later. During the TT seconds, you can choose some tasks to do in order to finish as many tasks as possible.

Input

Each test file contains a single test case. In each test file:

There are only two lines. The first line contains two positive integers n ( n \le 100 )n(n100) and T ( T \le 6000 )T(T6000). The second line contains nn positive integers t_1t1t_2, \cdots , t_n ( t_i \le 100 )t2,,tn(ti100).

Output

A number representing how many tasks can you finish before going to bed if you make the optimal decision.

样例输入

5 10 
8 3 4 2 1

样例输出

4

代码:

 1 //A-签到
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn=1e5+10;
 6 
 7 int a[maxn];
 8 
 9 int main()
10 {
11     int n,k;
12     scanf("%d%d",&n,&k);
13     for(int i=1;i<=n;i++){
14         scanf("%d",&a[i]);
15     }
16     sort(a+1,a+1+n);
17     int cnt=0,num=0;
18     for(int i=1;i<=n;i++){
19         if(cnt>=k){
20             printf("%d\n",num);
21             return 0;
22         }
23         if(cnt+a[i]<=k){
24             cnt+=a[i];num++;
25         }
26     }
27     printf("%d\n",num);
28 }

猜你喜欢

转载自www.cnblogs.com/ZERO-/p/11283254.html