Cod sursa(job #2929422)

Utilizator lucaxsofLuca Sofronie lucaxsof Data 25 octombrie 2022 20:40:55
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <math.h>
#include <vector>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
//#include <bits/stdc++.h>
using namespace std;

ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

#define fast ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

//to concatenate --> '+' OR append()
//the size --> length() OR size()
//string.insert(where, what)
//string.substr(start, how many)
//string.erase(start, how many);
//string.swap(another_string);

#define mod 1

#define maxx 5003

int obiects, weight;

int dp[maxx][maxx];

struct oop
{
	int p, w;
}Q[maxx];

bool comp(oop a, oop b)
{
	if (a.w != b.w)		return a.w < b.w;
	if (a.p != b.p)		return a.p < b.p;
}

void read()
{
	fin >> obiects >> weight;
	for (int i = 1; i <= obiects; ++i)
		fin >> Q[i].w >> Q[i].p;
	sort(Q + 1, Q + obiects + 1, comp);
}

//			

void knapsack()
{
	//sorteaza obiectele
	for (int i = 1; i <= obiects; ++i)
	{
		for (int j = 1; j < Q[i].w; ++j)
			dp[i][j] = dp[i - 1][j];
		for (int j = Q[i].w; j <= weight; ++j)
			dp[i][j] = max(dp[i - 1][j], Q[i].p + (dp[i - 1][j - Q[i].w]));
	}
	1;
}

void print()
{
	fout << dp[obiects][weight];
}

int main()
{
	read();
	knapsack();
	print();
}