Cod sursa(job #3222763)

Utilizator rainerretzler rainer Data 11 aprilie 2024 16:33:50
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include<fstream>

using namespace std;

int a[10010];
int maxG=0,G,mp;

ifstream fin("rucsac.in");

void process(int g, int v){
	int kk=0;
	int u=min(maxG,G-g);
	for(int i=u;i>0;i--){
		if(a[i]!=0){
			int t=a[i]+v;
			int z=i+g;
			a[z]=max(a[z],t);
			maxG=max(maxG,z);
			mp=max(mp,t);
		}
	}
	if(g<=G){
		a[g]=max(a[g],v);
		mp=max(mp,v);
		maxG=max(maxG,g);
	}
}


int main(){
	int n;
	fin>>n>>G;
	int w,p;
	while(n>0){
		--n;
		fin>>w>>p;
		process(w,p);

		// for(int i=0;i<=G;i++){
		// 	cout<<a[i]<<" ";
		// }
		// cout<<"\n"<<mp<<" "<<maxG<<"\n";

	}
	fin.close();
	ofstream fout("rucsac.out");



	fout<<mp<<"\n";
	fout.close();
	return 0;
}