Cod sursa(job #2151573)

Utilizator NeredesinI am not real Neredesin Data 4 martie 2018 17:16:41
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 75000;
const int VMAX = 200;
const int DIM = 2 * 1e6;

int n, g, res;
int ap[1 + VMAX];
int dp[1 + NMAX];
int sol[1 + NMAX];

int pos1;
char buff[DIM];

void read(int &x) {
  x = 0;

  while(!isdigit(buff[pos1])) {
    if(++pos1 == DIM) {
      in.read(buff, DIM);
      pos1 = 0;
    }
  }

  while(isdigit(buff[pos1])) {
    x = x * 10 + (buff[pos1] - '0');
    if(++pos1 == DIM) {
      in.read(buff, DIM);
      pos1 = 0;
    }
  }
}

int main()
{
  //in >> n >> g;
  read(n);
  read(g);
  for(int i = 1; i <= n; i++) {
    int x;
    //in >> x;
    read(x);
    ap[x]++;
  }

  dp[0] = 1;
  for(int i = VMAX; i >= 1; i--) {
    if(0 < ap[i]) {
      for(int k = g - i; k >= 0; k--) {
        if(dp[k] == 0)
           continue;
        for(int j = 1; j <= ap[i]; j++) {
          int p = i * j + k;
          if(p <= g && dp[p] == 0) {
            dp[p] = dp[k] + j;
            sol[p] = i;
            res = max(res, p);
          } else {
            break;
          }
        }
      }
    }
  }

  out << res << ' ' << dp[res] - 1 << '\n';

  while(res != 0) {
    out << sol[res] << '\n';
    res -= sol[res];
  }

  in.close();
  out.close();
  return 0;
}