Cod sursa(job #1539193)

Utilizator mihai.constantinConstantin Mihai mihai.constantin Data 30 noiembrie 2015 14:16:52
Problema Ghiozdan Scor 12
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <iostream>
#include <cstdio>
using namespace std;

FILE *in, *out;

const int N_max = 5000;
const int G_max = 10000;

int weight[N_max+1];
int sol[G_max+1]; // sol[i] = NR MINIM DE OBIECTE AVAND GREUTATEA i
int last[G_max+1];

int N, G, weight_max;

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", &weight[i]);

    last[0] = -1;

    for(i = 1; i <= N; i++)
        for(j = G - weight[i]; j >= 0; j--)
            if(last[j] != 0)
                if(last[j + weight[i]] == 0 || sol[j + weight[i]] > sol[j] + 1)
                {
                    sol[j + weight[i]] = sol[j] + 1;
                    last[j + weight[i]] = i;

                    if(weight_max < j + weight[i])
                        weight_max = j + weight[i];
                }

    cout << weight_max << " " << sol[weight_max];

    /*for(int i = 1; i <= G; i++) cout << i <<" ";
    cout <<'\n';
    for(int i = 1; i <= G; i++) cout << sol[i] <<" ";
    cout << '\n';
    for(int i = 1; i <= G; i++) cout << last[i] <<" ";*/

    return 0;
}