Cod sursa(job #2589883)

Utilizator RobertLearnsCDragomir Robert. RobertLearnsC Data 27 martie 2020 08:27:05
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
/** Complexitate
O(N * G)
**/
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream in("rucsac.in");
ofstream out("rucsac.out");
 
struct object {
    int weight, val;
} objects[5001];
 
long long n, maxWeight, ans[10001];
 
int main()
{
    in >> n >> maxWeight;
    for(int i = 1; i <= n; i++) {
        in >> objects[i].weight >> objects[i].val;
    }
    long long finalAns = -1;
    for(int i = 1; i <= n; i++) {
        for(int j = maxWeight - objects[i].weight; j >= 0; j--) {
            ans[j + objects[i].weight] = max(ans[j + objects[i].weight], ans[j] + objects[i].val);
            finalAns = max(finalAns, ans[j + objects[i].weight]);
        }
    }
    out << finalAns;
    return 0;
}