Cod sursa(job #752189)

Utilizator Victor10Oltean Victor Victor10 Data 28 mai 2012 01:53:16
Problema Energii Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <cstdio>
#include <algorithm>
using namespace std;

struct EnergieCostint {
	int e, c;
};
struct PotCostbool {
	bool OK;
	int c;
};

bool cmp (const EnergieCostint a, const EnergieCostint b) {
	if (a . e == b . e) return (a . c < b . c);
	return a . e < b . e;
}

EnergieCostint Gen [1005];
PotCostbool pot [100005];

int main () {
	
	freopen ("energii.in", "r", stdin);
	freopen ("energii.out", "w", stdout);
	
	int n, S, i, j, minn;
	
	scanf ("%d%d", &n, &S);
	
	for (i = 1; i <= n; ++ i)
		scanf ("%d%d", &Gen [i] . e, &Gen [i] . c);
	
	sort (Gen + 1, Gen + n + 1, cmp);
	
	pot [0] . OK = 1;
	for (i = 1; i <= n; ++ i) {
		for (j = 100005 - Gen [i] . e; j >= 0; -- j) {
			if (pot [j] . OK == 1) {
				pot [j + Gen [i] . e] . OK = 1;
				if ((pot [j + Gen [i] . e] . c != 0) && (pot [j] . c + Gen [i] . c < pot [j + Gen [i] . e] . c)) {
					pot [j + Gen [i] . e] . c = pot [j] . c + Gen [i] . c;
				}
				else
					if (pot [j + Gen [i] . e] . c == 0) {
						pot [j + Gen [i] . e] . c = pot [j] . c + Gen [i] . c;
					}
			}
		}
	}
	
	minn = 40000;
	for (i = S; i <= 100005; ++ i) {
		if (pot [i] . OK && pot [i] . c >= 1 && pot [i] . c < minn)
			minn = pot [i] . c;
	}
	
	if (minn == 40000)
		printf ("-1\n");
	else
		printf ("%d\n", minn);
}