Cod sursa(job #3036355)

Utilizator AndreiMLCChesauan Andrei AndreiMLC Data 24 martie 2023 11:08:37
Problema Problema rucsacului Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
// rucsac.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <fstream>

using namespace std;

ifstream f("rucsac.in");
ofstream g("rucsac.out");

int n, gre;
int v[1000], gr[1000];
int mat[1000][1000];

int main()
{
	f >> n >> gre;
	for (int i = 1; i <= n; i++)
	{
		f >> gr[i] >> v[i];
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= gre; j++)
		{
			if (gr[i] > j)
			{
				mat[i][j] = mat[i - 1][j];
			}
			else {
				mat[i][j] = max(mat[i - 1][j], mat[i - 1][j - gr[i]] + v[i]);
			}
		}
	}
	g << mat[n][gre];
}