Cod sursa(job #2272249)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 29 octombrie 2018 21:29:03
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>

using namespace std;

const int GMAX=5e7+2;

bool A[GMAX];

struct obiect
{
    int G, P;
};

int N, G, i, W, P, j;

int sol[GMAX], ans, s;

int main()
{
    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);

    scanf("%d%d", &N, &G);

    A[0]=1;

    for(i=1; i<=N; i++)
    {
        scanf("%d%d", &W, &P);

        for(j=G-W; j>=0; j--)
            if(A[j])
            {
                A[j+W]=true;
                sol[j+W]=max(sol[j+W], (sol[j]+P));
            }

        s+=W;
    }

    for(i=G; i>=0; i--)
        ans=max(ans, sol[i]);

    printf("%d\n", ans);

    return 0;
}