Cod sursa(job #1708697)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 27 mai 2016 20:10:55
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Lista lui wefgef Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

constexpr int MAX_N = 20000;
constexpr int MAX_G = 75000;
constexpr int MAX_W = 200;
constexpr int INF = numeric_limits<int>::max() / 2;

int dp[MAX_G + 1];
pair<int, int> backp[MAX_G + 1];
int cnt[MAX_W + 1];

int main()
{
    ifstream in("ghiozdan.in");
    ofstream out("ghiozdan.out");

    int N, G;
    in >> N >> G;

    for (int i = 1; i <= N; ++i)
    {
        int x;
        in >> x;
        cnt[x]++;
    }

    dp[0] = 1;

    for (int v = MAX_W; v >= 1; v--)
        if (cnt[v] > 0)
            for (int i = MAX_G; i >= 0; --i)
                if (dp[i] != 0)
                    for (int j = 1; j <= cnt[v] && i + j * v <= MAX_G; ++j)
                        if (dp[i + j * v] == 0)
                        {
                            dp[i + j * v] = dp[i] + j;
                            backp[i + j * v] = make_pair(v, j);
                        }
                        else
                            break;

    while (dp[G] == 0)
        G--;

    out << G << " " << dp[G] - 1 << "\n";

    while (G > 0)
    {
        auto p = backp[G];
        G -= p.first * p.second;

        while (p.second--)
            out << p.first << "\n";
    }

    return 0;
}