Cod sursa(job #2691213)

Utilizator walentines44Serban Valentin walentines44 Data 27 decembrie 2020 15:57:53
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

int dp[2][10001];
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

int main(){

	int N, W;
	fin >> N >> W;
	int weights[N + 1], profits[N + 1];
	weights[0] = 0;
	profits[0] = 0;
	for(int i = 1; i <= N; ++i){
		fin >> weights[i] >> profits[i];
	}
	for(int i = 0; i <= N; ++i){
		if(i % 2){
			for(int j = 0; j <= W; ++j){
				if(weights[i] <= j){
					dp[1][j] = max(dp[0][j], dp[0][j - weights[i]] + profits[i]);
				}
				else{
					dp[1][j] = dp[0][j];
				}
			}
		}
		else{
			for(int j = 0; j <= W; ++j){
				if(weights[i] <= j){
					dp[0][j] = max(dp[1][j], dp[1][j - weights[i]] + profits[i]);
				}
				else{
					dp[0][j] = dp[1][j];
				}
			}
		}
	}
	if(N % 2 == 0){
		fout << dp[0][W];
	}
	else{
		fout << dp[1][W];
	}

	return 0;
}