Cod sursa(job #686813)

Utilizator BarracudaFMI-Alex Dobrin Barracuda Data 21 februarie 2012 21:24:11
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include<fstream>
#define lim 5002
#define lim2 10010
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n,G,i,g,Profit[2][lim2],x;
struct dt {
	int w,p;
}
v[lim];
void read(){
	fin>>n>>G;
	for(int i=1;i<=n;i++)
		fin>>v[i].w>>v[i].p;
}
int max(int a,int b){
	if(a>b)
		return a;
	return b;
}
void solve(){
	x=0;
	for(int i=1;i<=n;x=1-x,i++){
		for(int g=0;g<=G;g++){
			if(v[i].w<=g){
				Profit[1-x][g]=max(Profit[x][g],Profit[x][g-v[i].w]+v[i].p);
			}
			else
				Profit[1-x][g]=Profit[x][g];
		}
	}
}
void print(){
	fout<<Profit[x][G];
}
int main (){
	read();
	solve();
	print();
	return 0;
}