Cod sursa(job #2569499)

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

const int NMAX=75005;
int dp[NMAX];

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<=G;i++)
        dp[i]=-1;
    dp[0]=0;
    for(int i=1;i<=n;i++){
        int g;
        scanf("%d", &g);
        for(int j=R;j>=0;j--){
            if(j+g<=G && dp[j]!=-1 && (dp[j]+1<dp[j+g] || dp[j+g]==-1)){
                dp[j+g]=dp[j]+1;
                if(j+g>R)
                    R=j+g;
            }
        }
    }
    int gmax=G;
    while(dp[gmax]==-1)
        gmax--;
    printf("%d %d\n", gmax, dp[gmax]);
    for(int i=gmax;i>=1;){
        int j=gmax-1;
        while(j>0 && dp[j]!=dp[i]-1)
            j--;
        printf("%d\n", i-j);
        i=j;
    }
    return 0;
}