Pagini recente » Cod sursa (job #783205) | Cod sursa (job #711485) | Cod sursa (job #29875) | Cod sursa (job #3183698) | Cod sursa (job #1248697)
#include<fstream>
using namespace std;
ifstream fi("ghiozdan.in");
ofstream fo("ghiozdan.out");
const int MAX_N = 20004;
const int MAX_G = 75004;
int greutate[MAX_N];
bool este[MAX_G];
int last[MAX_G];
int nr[MAX_G];
int sol,G;
int i,j,N;
int ok[205];
int main(){
fi>>N>>G;
for(i=1;i<=N;i++){ fi>>greutate[i]; ok[greutate[i]]++; }
este[0]=1;
last[0]=0;
nr[0]=0;
for(j=1;j<=G;j++) nr[j]=N+5;
for(i=1;i<=N;i++)
for(j=G;j>=greutate[i];j--)
if(este[j-greutate[i]] && (nr[j-greutate[i]]+1<nr[j]))
{
este[j]=1;
last[j]=greutate[i];
nr[j]=nr[j-greutate[i]]+1;
if(j>sol) sol=j;
}
fo<<sol<<" "<<nr[sol]<<"\n";
for(j=sol;j>=0;--j)
if(nr[j]+1==nr[sol] && ok[sol-j])
{
--ok[sol-j];
fo<<sol-j<<"\n";
sol=j;
}
fi.close();
fo.close();
return 0;
}