Cod sursa(job #1223964)

Utilizator catalincraciunCraciun Catalin catalincraciun Data 29 august 2014 11:56:14
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
// Craciun Catalin
//  Energii
//   Infoarena
#include <iostream>
#include <cstring>
#include <climits>
#include <fstream>

struct obj {
	
	int val, weight;
};

using namespace std;

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

#define GMax 10005
#define min(a,b) ((a<b)?(a):(b))

int V[GMax];
bool T[GMax];
int n,weight;
int sum=0;

int main() {
	
	f>>n>>weight;
	memset(V, 0, sizeof(V));
	memset(T, false, sizeof(T));
	
	T[0] = true;
	V[0] = 0;
	for (int i=1;i<=n;i++) {
		int oVal, oWeight;
		f>>oWeight>>oVal;
		sum+=oWeight;
		for (int j=weight; j>=oWeight;j--) {
			if (T[j-weight]) {
				if (T[j]) {
					if (V[j] > V[j-weight]+oVal)
						V[j] = V[j-weight]+oVal;
				} else {
					V[j] = V[j-weight]+oVal;
					T[j] = true;
				}
			}
		}
	}
	
	if (sum < weight)
		g<<-1<<'\n';
	else {
		int sol = INT_MAX;
		for (int i=weight;i<=sum;i++)
			if (T[i])
				sol = min(sol, V[i]);
			
		g<<sol<<'\n';
	}
	
	f.close();
	g.close();
	
	return 0;
}