Cod sursa(job #1964520)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 13 aprilie 2017 14:36:15
Problema Ghiozdan Scor 54
Compilator cpp Status done
Runda Lista lui wefgef Marime 1.1 kb
#include <fstream>
#define VAL 75005

using namespace std;

ifstream fin("ghiozdan.in");
ofstream fout("ghiozdan.out");

int N, G, i, j;
int dp[VAL], nr;
int last[VAL], mx;

int main()
{
    fin >> N >> G;
    for (i=1; i<=N; i++)
    {
        fin >> nr;
        for (j=G-nr; j>=0; j--)
        {
            if (dp[j]!=0)
            {
                if (dp[j+nr]==0)
                {
                    dp[j+nr]=dp[j]+1;
                    last[j+nr]=nr;
                    mx=max(mx, j+nr);
                }
                else
                {
                    if (dp[j+nr]>dp[j]+1)
                    {
                        dp[j+nr]=dp[j]+1;
                        last[j+nr]=nr;
                        mx=max(mx, j+nr);
                    }
                }
            }
            if (j==0)
            {
                mx=max(mx, nr);
                dp[nr]=1;
                last[nr]=nr;
            }
        }
    }
    fout << mx << " " << dp[mx] << '\n';
    while (mx!=0)
    {
        fout << last[mx] << '\n';
        mx-=last[mx];
    }
    fin.close();
    fout.close();
    return 0;
}