Cod sursa(job #126614)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 22 ianuarie 2008 16:01:22
Problema Ghiozdan Scor 6
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b)
{
   return *(int*)b - *(int*)a;
}

int v[2000], c[7500], n, g, nr[7500], sol[2000], x;

int main()
{
   freopen("ghiozdan.in","r",stdin);
   freopen("ghiozdan.out","w",stdout);
   int i, j;
   scanf ("%d %d", &n, &g);
   for (i = 1; i <= n; i++)    scanf("%d",&v[i]);

   qsort(v,n + 1, sizeof(int),cmp);

   c[0] = 1;

   for (i = 0; i < n; i++)
   {
      for (j = g - v[i]; j >= 0; j--)
	 if (c[j] && (nr[c[j+v[i]]] ==0 ||nr[c[j+v[i]]] > nr[j] + 1))  c[j + v[i]] = j+1, nr[j + v[i]] = nr[j] + 1;
   }

   i = g;
   while (!c[i]) i--;
   printf("%d %d\n",i,nr[i]);
   while (i)
   {
      x++;
      sol[x - 1] = i;
      i = c[i];
   }

   for (i = x; i>=1; i--) printf("%d\n",sol[i]);
   return 0;
}