Pagini recente » Cod sursa (job #2780507) | Cod sursa (job #2499926) | Cod sursa (job #1388810) | Cod sursa (job #1265173) | Cod sursa (job #952375)
Cod sursa(job #952375)
#include <fstream>
#include <iostream>
#include <array>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>
#define MAX_SIZE 75001
#define MAX_WEIGHT 201
using namespace std;
array<short, MAX_SIZE> knapsack;
array<short, MAX_SIZE> solution;
array<short, MAX_WEIGHT> items;
int main()
{
int N, G;
fstream fin("ghiozdan.in", fstream::in);
fstream fout("ghiozdan.out", fstream::out);
fin >> N >> G;
//cout << N << " " << G << endl;
for (int i=0; i<N; ++i)
{
int item;
fin >> item;
items[item]++;
}
knapsack[0] = 1;
items[0] = 1;
for (int i=200; i>=0; --i)
{
if (items[i] == 0) continue;
//cout << "Items " << i << " " << items[i] << endl;
for (int w=G-i; w>=0; --w)
{
if (knapsack[w] == 0) continue;
for (int k=1; k<=items[i] && w + k*i <= G && knapsack[w + k*i] == 0; ++k)
{
//cout << k << " " << i << " " << w << " " << knapsack[w + k*i] << endl;
knapsack[w + k*i] = knapsack[w] + k;
solution[w + k*i] = i;
}
}
}
for (int w=G; w>0; --w)
{
if (knapsack[w]-1 > 0)
{
fout << w << " " << knapsack[w]-1 << "\n";
short i = w;
while (i != 0)
{
fout << solution[i] << "\n";
i -= solution[i];
}
return 0;
}
}
return 0;
}