Cod sursa(job #1540167)

Utilizator vapopescuVlad Andrei Popescu vapopescu Data 2 decembrie 2015 12:26:43
Problema Ghiozdan Scor 54
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <iostream>
#include <fstream>
#define N_MAX 20001
#define G_MAX 75001
using namespace std;

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

int g[N_MAX], an[G_MAX], au[G_MAX];

int main() {
	int N, G;

	au[0] = 0;
	fin >> N >> G;
	for (int i = 1; i <= N; i++) {
		fin >> g[i];
		for (int j = G - g[i]; j >= 0; j--) {
			if (an[j] != 0 || j == 0) {
				int k = j + g[i];

				if (an[k] == 0 || an[j] + 1 < an[k]) {
					an[k] = an[j] + 1;
					au[k] = i;
				}
			}
		}
	}

	int k = G;
	while (an[k] == 0)
		k--;
	fout << k << " " << an[k] << "\n";

	while (k > 0) {
		fout << g[au[k]] << "\n";
		k -= g[au[k]];
	}

	return 0;
}