Discuss: in this problem just sort the weight and then add until weight less or equal maximum weight.
in increase the value of cnt
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long t,n,w,m,i,j;
cin >>t;
while(t--)
{
vector<long long>vec;
cin >>n>>m;
for(i=0;i<n;i++)
{
cin >>j;
vec.push_back(j);
}
sort(vec.begin(),vec.end());
long long sum=0,cnt=0;
for(i=0;i<n;i++)
{
sum+=vec[i];
if(sum<=m)
{
cnt++;
}
else
break;
}
cout <<cnt<<endl;
}
return 0;
}
in increase the value of cnt
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long t,n,w,m,i,j;
cin >>t;
while(t--)
{
vector<long long>vec;
cin >>n>>m;
for(i=0;i<n;i++)
{
cin >>j;
vec.push_back(j);
}
sort(vec.begin(),vec.end());
long long sum=0,cnt=0;
for(i=0;i<n;i++)
{
sum+=vec[i];
if(sum<=m)
{
cnt++;
}
else
break;
}
cout <<cnt<<endl;
}
return 0;
}
No comments:
Post a Comment