Cod sursa(job #2957972)

Utilizator YosifIosif Andrei Stefan Yosif Data 23 decembrie 2022 22:43:51
Problema Ghiozdan Scor 48
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

int *a, *b;
int n, g;

int main()
{
    fin >> n >> g;

    if (g == 0)
    {
        fout << "0 0";
        return 0;
    }

    a = new int[75002]();
    b = new int[75002]();

    for (int i = 1; i <= n; i++)
    {
        int x;
        fin >> x;
        x = min(x, g + 1);

        for (int j = 1; j < x; j++)
            b[j] = a[j];

        b[x] = 1;

        for (int j = x + 1; j <= g; j++)
        {
            b[j] = a[j];
            if (a[j - x] && (!b[j] || b[j] > a[j - x] + 1))
                b[j] = a[j - x] + 1;
        }

        swap(a, b);
    }

    int i = g;

    while (i >= 1 && !a[i])
        i--;

    fout << i << ' ' << a[i] << '\n';

    return 0;
}