Cod sursa(job #19039)

Utilizator alextheroTandrau Alexandru alexthero Data 18 februarie 2007 18:13:13
Problema Ghiozdan Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <cstdio>

#define nmax 20005
#define gmax 75005
#define vmax 205

int c[nmax],cs[nmax],a[nmax],v[vmax],prev[nmax];
int n,g;

int main() {
    freopen("ghiozdan.in","r",stdin);
    freopen("ghiozdan.out","w",stdout);
    
    scanf("%d%d",&n,&g);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&a[i]);    
        v[a[i]]++;
    }

    n = 0;

    for(int i = 1; i <= 200; i++) 
        while(v[i] > 0) {
            a[++n] = i;
            v[i]--;  
        }

    int maxim = 0,cate = 0,poz = 0;

    for(int i = n; i >= 1; i--) {
        if(a[i] > g) continue;
        c[i] = a[i];
        prev[i] = 0;
        cs[i] = 1;
        for(int j = i + 1; j <= n; j++) 
            if(c[j] + a[i] > c[i] && c[j] + a[i] <= g) {
                c[i] = c[j] + a[i]; 
                cs[i] = cs[j] + 1;
                prev[i] = j;
            }
            else if(c[j] + a[i] == c[i]) {
                if(cs[j] + 1 < cs[i]) {
                    cs[i] = cs[j] + 1;
                    prev[i] = j;
                }
            }
        if(c[i] > maxim) {
            maxim = c[i];       
            cate = cs[i];
            poz = i;
        }
        else if(c[i] == maxim) {
            if(cs[i] < cate) {
                cate = cs[i];
                poz = i;
            }
        }
    }
    printf("%d %d\n",maxim,cate);
    while(poz != 0) {
        printf("%d\n",a[poz]);
        poz = prev[poz];    
    }

    return 0;
}