Cod sursa(job #2407567)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 16 aprilie 2019 23:10:21
Problema Ghiozdan Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");
int n, W;
int fv[210];
int last[75100];
int dp[75100] = {1}, ap[75100];
int main()
{
    f >> n >> W;
    for(int i=1, x; i<=n; i++)
        f >> x, fv[x]++;
    for(int i=200; i; i--)
    {
        if(!fv[i])
            continue;
        for(int j=W; j>=0; j--)
            if(dp[j])
                for(int d=1; d<=fv[i] && j+d*i <= W && !dp[j + d*i]; d++)
                    dp[j+d*i] = dp[j] + d, ap[j+d*i] = i;
    }
    int j = W;
    while (j && !dp[j])
        j--;
    g << j << " " << dp[j]-1 << '\n';
    while(j > 0)
    {
        g << ap[j] << '\n';
        j -= ap[j];
    }
    return 0;
}