Cod sursa(job #362210)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 8 noiembrie 2009 15:58:16
Problema Economie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,nr,lim;
int v[1002],s[50002],sol[1002],o[1002];

bool cmp(int i, int j)
{
	return v[i]<v[j];
}

int main()
{
	freopen("economie.in","r",stdin);
	freopen("economie.out","w",stdout);
	scanf("%d",&n);
	int i,j,max=-1;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&v[i]);
		if(v[i]>max)
			max=v[i];
		o[i]=i;
	}
	sort(o+1,o+n+1,cmp);
	s[0]=1;
	for(i=1;i<=n;i++)
		if(!s[v[o[i]]])
		{
			sol[++nr]=v[o[i]];
			lim=max-v[o[i]];
			for(j=0;j<=lim;j++)
				if(s[j] && !s[j+v[o[i]]])
					s[j+v[o[i]]]=1;
		}
	printf("%d\n",nr);
	for(i=1;i<=nr;i++)
		printf("%d\n",sol[i]);
	return 0;
}