Cod sursa(job #2794273)

Utilizator Andrei_Tud1Andrei Tudorache Andrei_Tud1 Data 4 noiembrie 2021 16:27:25
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, i, gmax, j, optim[10001], sol;
struct obiect{
    int g;
    int v;
}a[5001];

int main()
{
    fin >> n >> gmax;
    for(i = 1; i <= n; i++)
        fin >> a[i].g >> a[i].v;

    for(i = 1; i <= n; i++)
    {
        for(j = gmax - a[i].g; j >= 0; j--)
        {
            if(optim[j + a[i].g] < optim[j] + a[i].v)
            {
                optim[j + a[i].g] = optim[j] + a[i].v;
                if(optim[j+a[i].g] > sol)
                    sol = optim[j+a[i].g];
            }
        }
    }
    fout << sol;
    return 0;
}