Cod sursa(job #2201216)

Utilizator DimaTCDima Trubca DimaTC Data 3 mai 2018 21:55:08
Problema Ghiozdan Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include<bits/stdc++.h>
#define NMAX 100000
using namespace std;

int d[75250],t[1000000][2], a[20005], p[75250];
int n,g,k,x;

int main() {
	ifstream cin("t.in");
    ofstream cout("ghiozdan.out");
	cin>>n>>g;
	for (int i=1; i<=n; i++) cin>>a[i];
	sort(a+1,a+1+n, greater<int>());
	for (int i=1; i<=n; i++) {
		for (int j=g-a[i]; j>=1; j--) {
			if (d[j] && (!d[j + a[i]] || d[j + a[i]]>d[j] + 1)) {
				d[j + a[i]] = d[j] + 1;
				t[++k][0] = p[j]; t[k][1] = a[i];
				p[j + a[i]] = k;
			} else if (d[j] && a[i] + j <=g) break;
		}
		d[a[i]] = 1; t[++k][0] = 0; t[k][1] = a[i]; p[a[i]] = k;
	}
	
	int i=g; while (!d[i]) i--;
	cout<<i<<" "<<d[i]<<'\n';  x=p[i];
	while (x) {
		cout<<t[x][1]<<"\n"; x = t[x][0];
	} 
	return 0;
}