Cod sursa(job #2652363)

Utilizator rARES_4Popa Rares rARES_4 Data 24 septembrie 2020 19:26:47
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include<fstream>
#include<iostream>
#include <algorithm>
using namespace std;
ifstream f ("a.in");
ofstream g("a.out");
long long n,gmax,rasp;
long long matr[5001][10001];
int greutati[5001],costuri[5001];
void afla_cale(int x,int y)
{
    if(matr[x-1][y] == matr[x][y])
        afla_cale(x-1,y);
    else
        if(matr[x-1][y-greutati[x]] == matr[x][y]-costuri[x])
            {
                afla_cale(x-1,y-greutati[x]);
                cout << x  << endl;
            }
}
int main()
{
    f >> n >> gmax;
    for(int i = 1;i<=n;i++)
    {
        long long gr,cost;
        f >> gr>> cost;
        greutati[i] = gr;
        costuri[i] = cost;
        for (int j=1; j<gr; j++)
            {
                matr[i][j]=matr[i-1][j];
            }
        for (int j=gr;j<=gmax; j++)
            {
                matr[i][j] = max(matr[i-1][j], matr[i - 1][j - gr] + cost);
            }
    }
   afla_cale(n,gmax);

}