Cod sursa(job #652430)

Utilizator luckyme91wiz kid luckyme91 Data 24 decembrie 2011 14:03:08
Problema Ghiozdan Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.25 kb
/* 
 * File:   main.c
 * Author: mihai
 *
 * Created on December 23, 2011, 11:47 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define max(a, b) (a) > (b) ? (a) : (b)

/*
 * 
 */
int main(int argc, char** argv) {
    freopen("ghiozdan.in", "r", stdin);
    freopen("ghiozdan.out", "w", stdout);


    int i, j, k, x, gmax = 0, ss = 0;
    int *c = (int*) calloc (201, sizeof(int));
    int *s = (int*) calloc (75000, sizeof(int));
    int *t = (int*) calloc (75000, sizeof (int));
    const int n, g;
    scanf("%d %d", &n, &g);
    for (i = 0; i < n; i++) {
        scanf("%d", &x);
        c[x]++;
        if (x > gmax) gmax = x;
    }
    s[0] = 1;
    for (i = gmax; i; --i)
        if (c[i])
            for (j = g - i; j >= 0; --j)
                if (s[j])
                    for (k = 1; k <= c[i] && j + k * i <= g && !s[j + k * i]; ++k) {
                        s[j + k * i] = s[j + (k - 1) * i] + 1;
                        t[j + k * i] = i;
                        if (j + k * i > ss)
                            ss = j + k * i;
                    }
    --s[ss];
    printf("%d %d\n", ss, s[ss]);
    for (i = ss; i; i -= t[i])
        printf("%d\n", t[i]);


    return 0;

}