Cod sursa(job #235797)

Utilizator Bogdan_tmmTirca Bogdan Bogdan_tmm Data 25 decembrie 2008 21:25:50
Problema Economie Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include<iostream>
#include<stdio.h>
FILE *f=fopen("economie.in","r"),*g=fopen("economie.out","w");
unsigned short v[50001];long a[1001],max,aux[1001],min=0,s=0,nr=0,j,i,n;
int divide(int st,int dr)
{
 long aux=a[st];
 while(st<dr)
 {
  while(st<dr&&a[dr]>=aux)
   dr--;
  a[st]=a[dr];
  while(st<dr&&a[st]<=aux)
   st++;
  a[dr]=a[st];
 }
a[st]=aux;
return st;
}
void qsort(int st,int dr)
{
 int m=divide(st,dr);
 if(m-1>st) divide(st,m-1);
 if(m+1<dr) divide(m+1,dr);
}
int main()
{
 fscanf(f,"%d",&n);
 for(i=1;i<=n;i++)
 {
  fscanf(f,"%ld",&a[i]); max=max>a[i]?max:a[i];
 }
 qsort(1,n);
 v[0]=1;
 for(i=1;i<=n;i++)
 {
  if(v[a[i]]==0)
  {
   nr++;
   aux[nr]=a[i];
   v[a[i]]=1;
  for(j=a[i]+1;j<=max;j++)
  {
   if(v[j-a[i]]==1)
    v[j]=1;
  }
  }
 }
 fprintf(g,"%ld\n",nr);
 for(i=1;i<=nr;i++)
   fprintf(g,"%ld\n",aux[i]);
return 0;
}