Cod sursa(job #656338)

Utilizator vendettaSalajan Razvan vendetta Data 4 ianuarie 2012 14:46:14
Problema Ghiozdan Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#define nmax  20001
#define wmax  202
#define gmax  75001

using namespace std;

int w[wmax];
int sol[gmax];
int t[gmax];
int n, K;
int Gmax;
int mx;

ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");

void citeste(){

   f>>n>>K;
   for(int i=1; i<=n; ++i){
      int x;
      f>>x;
      ++w[x];
      if (mx < x) mx = x;
   }

}

void rezolva(){

   sol[0] = 1;
   //t[0] = 0;
   for(int i=mx; i>=1; --i)
      if (w[i])
         for(int j=K-i; j>=0; --j){
            if (sol[j])
               for(int k=1; k<=w[i]; ++k)
                  if (j+i*k <= K && sol[j+i*k]==0){
                     sol[j+i*k] = sol[j] + k;
                     t[j+i*k] = i ;
                     if (Gmax < j+i*k) Gmax = j+i*k;
                  }
         }

   while (!sol[Gmax]) --Gmax;

}


void scrie(){

   g<<Gmax<<" "<<--sol[Gmax]<<"\n";

   for(int i=Gmax; i; i = i-t[i])
      g<<t[i]<<"\n";
}

int main(){

   citeste();
   rezolva();
   scrie();

   f.close();
   g.close();
   return 0;

}