Cod sursa(job #18384)

Utilizator nemesisIchim Alexandru Eugen nemesis Data 18 februarie 2007 11:53:01
Problema Ghiozdan Scor 80
Compilator cpp Status done
Runda preONI 2007, Runda 2, Clasele 11-12 Marime 1.12 kb
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int n, gdat;
struct nod {int x;} v[25050];

int comp(const void *i, const void *j)
{
  nod *ei= (nod*)i,
      *ej= (nod*)j;
  return ej->x - ei->x;
}

void solve()
{
  int s[75205], pred[75205];
  int M=0;
 
  memset(s, 0x3f3f3f3f, sizeof(s));
  s[0]=0;
  pred[0]=0;
  for(int i=1; i<=n; ++i) {
    if( M>gdat) M=gdat;
    for(int j=M; j>=0; --j) {
      if( s[gdat]!=0x3f3f3f3f) { i=n; break; }
//      if( s[j]+1 < s[j+v[i].x]) {
        if( s[j]!=0x3f3f3f3f && s[j + v[i].x]==0x3f3f3f3f) {
        s[j+v[i].x]= s[j]+1;
        pred[j+v[i].x]=i;
        if( j+v[i].x > M) M= j+v[i].x;
      }
    }
  }

  for(int i=gdat; i>=0; --i) if( s[i]!=0x3f3f3f3f) {gdat=i; break;}
  freopen("ghiozdan.out","w",stdout);
  printf("%d %d\n",gdat,s[gdat]);
  M=gdat;
  while(pred[M]) {
      printf("%d\n",v[pred[M]].x);
      M=M-v[pred[M]].x;
    }
}


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

  qsort(v+1, n, sizeof(nod), comp);

  solve();
  
  return 0;
}