Cod sursa(job #2798931)

Utilizator mariusvicolVicol Marius mariusvicol Data 12 noiembrie 2021 09:37:34
Problema Ghiozdan Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");

int frec[100000],n,gh,x,solutie[250000],dp[200000],k;
int main()
{
    f>>n>>gh;
    for(int i=1; i<=n; i++)
    {
        f>>x;
        frec[x]++;

    }
    dp[0]=1;
    for(int i=200; i>=1; i--)
    {
        for(int j=gh; j>=0; j--)
        {
            if(dp[j])
            {
                for(k=1; k*i+j<=gh&&k<=frec[i]; k++)
                {
                    if(dp[k*i+j])
                    {
                        break;
                    }
                    dp[k*i+j]=dp[j]+k;
                    solutie[i*k+j]=i;
                }
            }
        }

    }
    while(!dp[gh])
        gh=gh-1;
    g<<gh<<" "<<dp[gh]-1<<" "<<endl;
    while(gh)
    {
        g<<solutie[gh]<<endl;
        gh=gh-solutie[gh];

    }

}