Cod sursa(job #2721922)

Utilizator sichetpaulSichet Paul sichetpaul Data 12 martie 2021 14:17:21
Problema Ghiozdan Scor 74
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>
#define Nmax 75002
#define Vmax 200
using namespace std;

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

int N, G;
int last[Nmax], fr[Vmax], nr[Nmax];
bool dp[Nmax];
vector<int> points;

int main()
{
    fin >> N >> G;
    for (int i = 1; i <= N; ++i) {
        int x;
        fin >> x;
        ++fr[x];
    }

    dp[0] = 1;
    for (int weight = 1; weight <= Vmax; ++weight)
    if (fr[weight]) {
        points.clear();
        for (int i = G - weight; i >= 0; --i)
            if (dp[i]) points.push_back(i);

        for (auto it: points) {
            int i = it + weight, j = 1;
            while (i <= G && j <= fr[weight]) {
                if (dp[i] == 0 || (dp[i] && nr[i - weight] + 1 < nr[i])) {
                    dp[i] = 1;
                    nr[i] = nr[i - weight] + 1;
                    last[i] = i - weight;
                }
                i += weight;
                ++j;
            }
        }
    }

    int idx = 0;
    for (int i = 1; i <= G; ++i)
        if (dp[i]) idx = i;
    fout << idx << " " << nr[idx] <<  '\n';
    while (idx)
        fout << idx - last[idx] << '\n', idx = last[idx];

    return 0;
}