Untitled

 avatar
unknown
plain_text
2 years ago
943 B
5
Indexable
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 1e5+5;
pair<int,int> numeros[100+5];
vector<vector<ll>> calculado(100+5,vector<ll>(MAX,-1));

ll recur(int i, int peso){
    if(i <= 0){
        return 0;
    }
    if(calculado[i][peso] =! -1){
        return calculado[i][peso];
    }
    ll esquerda = recur(i-1,peso);
    if(peso-numeros[i].first < 0){
        calculado[i][peso] = esquerda;
        return esquerda;
    }
    ll direita = recur(i-1, peso-numeros[i].first) + numeros[i].second;
    calculado[i][peso] = max(direita,esquerda);
    return calculado[i][peso];
}

main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int n, capacidade; cin >> n >> capacidade;
    int pesoTotal = 0;
    for(int i =1;i<=n;i++){
        int peso, valor; cin >> peso >> valor;
        numeros[i] = {peso,valor};
    }
    cout << recur(n,capacidade);
}
Editor is loading...