Cod sursa(job #2569630)

Utilizator rebecca0312Andrei Rebecca rebecca0312 Data 4 martie 2020 12:56:04
Problema Ghiozdan Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include<bits/stdc++.h>
using namespace std;

const int NMAX=75005;
const int VMAX=205;

int dp[NMAX],last[NMAX],f[VMAX];

int main(){
    freopen("ghiozdan.in","r",stdin);
    freopen("ghiozdan.out","w",stdout);
    int n,G,R=0;
    scanf("%d%d", &n, &G);
    for(int i=1;i<=n;i++){
        int g;
        scanf("%d", &g);
        f[g]++;
    }
    for(int i=VMAX;i>=1;i--){
        for(int j=G;j>=0;j--){
            if(!dp[j])
                continue;
            for(int d=1;d*i+j<=G && d<=f[i];d++){
                if(dp[d*i+j])
                    break;
                dp[d*i+j]=dp[j]+d;
                last[d*i+j]=i;
            }
        }
    }
    int gmax=G;
    while(dp[gmax]==-1)
        gmax--;
    printf("%d %d\n", gmax, dp[gmax]);
    for(int i=gmax;i>=1;){
        printf("%d\n", i-last[i]);
        i=last[i];
    }
    return 0;
}