Cod sursa(job #3239420)

Utilizator CondoracheAlexandruCondorache Alexandru CondoracheAlexandru Data 5 august 2024 15:43:30
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define all(a) (a).begin(),(a).end()
using namespace std;
const ll maxn = 2e5 + 5;
const ll mod = 1e9 + 7;

ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
 
void solve() {
    int n, g;
    fin >> n >> g;
    vector<int> w(n), p(n);
    for (int i = 0; i < n; i++) {
        fin >> w[i] >> p[i];
    }
    vector<int>dp(g + 1, 0);
    for (int i = 1; i <= n; i++) {
        for (int j = g; j >= 0; j--) {
            if (w[i - 1] <= j) {
                dp[j] = max(dp[j], dp[j - w[i - 1]] + p[i - 1]);
            }
        }
    }
    fout << dp[g] << endl;
}
 
int main() {
    ll t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}