Pagini recente » Cod sursa (job #3235938) | Cod sursa (job #188010) | Cod sursa (job #3202551) | Cod sursa (job #2049257) | Cod sursa (job #3246539)
#include <stdio.h>
#include <stdlib.h>
#define MAXN 20001
#define MAXG 75001
int v[MAXN];
int dp[MAXG];
int count[MAXG];
int main() {
FILE *fin, *fout;
int n,g,i,j,cop,maxim,cnt;
fin=fopen("ghiozdan.in","r");
fout=fopen("ghiozdan.out","w");
fscanf(fin, "%d %d", &n, &g);
for (i=0; i<n; i++) {
fscanf(fin, "%d", &v[i]);
}
for (i=0; i<=g; i++) {
dp[i]=0;
count[i]=MAXN;
}
count[0]=0;
for (i=0; i<n; i++) {
for (j=g; j>=v[i]; j--) {
cop=dp[j-v[i]]+v[i];
if (cop>dp[j]) {
dp[j]=cop;
count[j]=count[j-v[i]]+1;
} else if (cop==dp[j] && count[j-v[i]]+1<count[j]) {
count[j]=count[j-v[i]]+1;
}
}
}
maxim=0;
cnt=MAXN;
for (i=0; i<=g; i++) {
if (dp[i]>maxim) {
maxim=dp[i];
cnt=count[i];
} else if (dp[i]==maxim && count[i]<cnt) {
cnt=count[i];
}
}
fprintf(fout, "%d %d\n", maxim, cnt);
cop=maxim;
for (i=0; i<n && cop>0; i++) {
while (cop>=v[i] && dp[cop]==dp[cop-v[i]]+v[i] && count[cop]==count[cop-v[i]]+1) {
fprintf(fout, "%d\n", v[i]);
cop-=v[i];
}
}
fclose(fin);
fclose(fout);
return 0;
}