Cod sursa(job #1549659)

Utilizator lraduRadu Lucut lradu Data 12 decembrie 2015 16:43:22
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <queue>

#define LL long long
#define pb push_back
#define lm 5001
#define MOD 1000000007

using namespace std;


int w[lm],v[lm];
int opt[10001];

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

    int n,g,res=0;

    scanf("%d %d",&n,&g);

    for(int i=1; i<=n; i++)
    {
        scanf("%d %d",&w[i],&v[i]);
    }

    for(int i=1; i<=n; i++)
        for(int j= g-w[i] ; j>=0; j--)
        {
            if(opt[j+w[i]] < opt[j]+v[i])
                {
                    opt[j+w[i]]=opt[j] + v[i];
                    if(opt[j+w[i]]>res)
                        res = opt[j+w[i]];
                }
        }
    printf("%d",res);
    return 0;
}