Cod sursa(job #2854529)

Utilizator SofeiAndreiSofei Andrei SofeiAndrei Data 21 februarie 2022 14:45:05
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
struct obiect{
    int greutate;
    int pret;
}obiecte[5001];
int n,W,aux;
int main()
{
    f>>n>>W;
    int profit[2][W+1];
    for(int i=0;i<=W;i++){
        profit[0][i]=0;
        profit[1][i]=0;
    }
    for(int i=1;i<=n;i++){
            f>>obiecte[i].greutate;
            f>>obiecte[i].pret;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=W;j++){
            if(j-obiecte[i].greutate<0){
                profit[1][j]=profit[0][j];
            }
            else{
                profit[0][j]=profit[1][j];
                profit[1][j]=max(profit[0][j], profit[0][j-obiecte[i].greutate]+obiecte[i].pret);
            }
        }
        for(int j=1;j<=W;j++){
            profit[0][j]=profit[1][j];
        }
    }
    /*
    for(int j=1;j<=W;j++){
            cout<<profit[0][j]<<" ";
    }
    cout<<'\n';
    for(int j=1;j<=W;j++){
            cout<<profit[1][j]<<" ";
    }
    */
    g<<profit[1][W];
    return 0;
}