Cod sursa(job #120119)

Utilizator andrei.12Andrei Parvu andrei.12 Data 4 ianuarie 2008 12:01:58
Problema Economie Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include<stdio.h>
#include<algorithm>
#define lg1 1005
#define lg2 50005

using namespace std;

int n, i, j, v[lg1], q[lg2], mx, d[lg2], x, fst[lg2], sol[lg1], w[lg1];
int main()
{
	freopen("economie.in", "rt", stdin);
	freopen("economie.out", "wt", stdout);
	
	scanf("%d", &n);
	
	for (i = 1; i <= n; i ++){
		scanf("%d", &v[i]);
		q[v[i]] = 1;
		mx = max(mx, v[i]);
	}
	
	sort(v+1, v+n+1);
	d[0] = 1;
	int nrs = 0, s = 0;
	for (i = 1; i <= n && !s; i ++)
		for (j = 0; j <= mx && !s; j ++)
			if (d[j]){
				x = j + v[i];
				if (x <= mx){
					d[x] = 1;
					if (q[x] == 1 && !fst[x]){
						sol[++sol[0]] = v[i];
						fst[x] = 1;
						nrs ++;
					}
				}
				if (nrs == n)
					s = 1;
			}
	
	sort(sol+1, sol+sol[0]+1);
	
	x = 0;
	for (i = 1; i <= sol[0]; i ++)
		if (sol[i] != x){
			w[++w[0]] = sol[i];
			x = sol[i];
		}
	
	printf("%d\n", w[0]);
	for (i = 1; i <= w[0]; i ++)
		printf("%d\n", w[i]);
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}