Cod sursa(job #3324027)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 20 noiembrie 2025 19:07:22
Problema Ghiozdan Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <bits/stdc++.h>
#define cin fin
#define cout fout

using namespace std;

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

const int GMAX = 75000;
const int VMAX = 200;
const int INF = 1e9;

int n, g, start;
int freq[VMAX + 1];
int dp[GMAX + 1];
int parent[GMAX + 1];
vector<int> answer;

int main() {
    cin >> n >> g;
    for(int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        freq[x]++;
    }

    fill(dp + 1, dp + g + 1, INF);
    for(int i = VMAX; i >= 1; i--) {
        if(!freq[i]) {
            continue;
        }

        int f = freq[i];
        int p = 1;
        while(f) {
            for(int j = g; j >= p * i; j--) {
                int here = dp[j - p * i] + p;
                if(here < dp[j]) {
                    dp[j] = here;
                    parent[j] = j - i;
                }
            }
            f -= p;
            p = (p * 2 > f ? f : p * 2);
        }
    }
    for(int i = g; i >= 0; i--) {
        if(dp[i] != INF) {
            start = i;
            break;
        }
    }

    cout << start << ' ' << dp[start] << '\n';
    while(start) {
        cout << start - parent[start] << '\n';
        start = parent[start];
    }
    return 0;
}