A. Amr and Music
time limit per test
memory limit per test
input
output
Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea.
n instruments, it takes ai days to learn i-th instrument. Being busy, Amr dedicated k
Amr asked for your help to distribute his free days between instruments so that he can achieve his goal.
Input
n, k (1 ≤ n ≤ 100, 0 ≤ k ≤ 10 000), the number of instruments and number of days respectively.
n integers ai (1 ≤ ai), representing number of days required to learn the i-th instrument.
Output
m
m
if there are multiple optimal solutions output any. It is not necessary to use all days for studying.
Sample test(s)
input
4 10 4 3 1 2
output
4 1 2 3 4
input
5 6 4 3 1 1 2
output
3 1 3 4
input
1 3 4
output
0
Note
4
{2, 3, 5} or {3, 4, 5}.
In the third test Amr doesn't have enough time to learn the only presented instrument.
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int n,m;
struct node
{
int x,y;
}a[14100];
int cmp(const void *a,const void *b)
{
struct node *aa,*bb;
aa=(struct node *)a;
bb=(struct node *)b;
return aa->x-bb->x;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i].x);
a[i].y = i+1;
}
qsort(a,n,sizeof(a[0]),cmp);
int b[10010];
int t = 0;
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{
if(m>=a[i].x)
{
m = m-a[i].x;
b[t++] = a[i].y;
}
else
{
break;
}
}
sort(b,b+t);
printf("%d\n",t);
for(int i=0;i<t;i++)
{
if(i == t-1)
{
printf("%d\n",b[i]);
}
else
{
printf("%d ",b[i]);
}
}
}
return 0;
}