Cod sursa(job #2759677)

Utilizator mihnea_buzoiuMihnea Buzoiu mihnea_buzoiu Data 19 iunie 2021 18:39:21
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
//
//  problema_rucsacului.cpp
//  probleme
//
//  Created by Mihnea Buzoiu on 6/6/21.
//

#include <stdio.h>
#include <iostream>

const int MAXG = 5e4 + 2;

int best[MAXG];

int max(int x, int y){
    return x > y ? x : y;
}
 
int main()
{
    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);
    
    int n, g;
    scanf("%d %d", &n, &g);
    
    for (int i=0; i<n; i++){
        int w, p;
        scanf("%d %d", &w, &p);
        
        for (int j=g; j>=w; j--)
            best[j] = max(best[j], best[j-w] + p);
    }
    
    
    printf("%d", best[g]);
}