Cod sursa(job #2379096)

Utilizator mihaimusat.1998Musat Mihai-Robert mihaimusat.1998 Data 12 martie 2019 21:25:11
Problema Problema rucsacului Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <bits/stdc++.h>

#define nmax 5001
#define gmax 10001

using namespace std;

int main()
{
    ifstream fin("rucsac.in");
    ofstream fout("rucsac.out");

    int n, g;
    int w[nmax], p[nmax], dp[gmax];

    fin>>n>>g;
    for(int i=1; i<=n; i++) {
        fin>>w[i]>>p[i];
    }

    int sol = 0;
    for(int i=1; i<=n; i++) {
        for(int j=g; j>=w[i]; j--) {
            if(dp[j] < dp[j-w[i]] + p[i])
                dp[j] = dp[j-w[i]]+p[i];
            if(dp[j]>sol) {
                sol=dp[j];
            }
        }
    }

    fout<<sol<<endl;

    return 0;
}