Pagini recente » Cod sursa (job #1110814) | Cod sursa (job #1276040) | Cod sursa (job #2013057) | Cod sursa (job #2073864) | Cod sursa (job #1223964)
// 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;
}