Cod sursa(job #1875867)

Utilizator teodor99Theodor Ciuciuc teodor99 Data 11 februarie 2017 18:16:12
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>

using namespace std;

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

int a[2][10001];

struct abc{
int greutate;
int profit;
}x[5001];

int main() {
    int n, g;
    fin >> n >> g;
    for (int i = 1; i <= n; i++)
        fin >> x[i].greutate >> x[i].profit;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= g; j++)
            if (j >= x[i].greutate)
                a[i % 2][j] = max (a[(i - 1) % 2][j], a[(i - 1) % 2][j - x[i].greutate] + x[i].profit);
            else
                a[i % 2][j] = a[(i - 1) % 2][j];
    fout << a[n % 2][g];
    return 0;
}