Pagini recente » Cod sursa (job #2272330) | Cod sursa (job #1994103) | Cod sursa (job #352906) | Cod sursa (job #694680) | Cod sursa (job #2212187)
#include <iostream>
#include <fstream>
#define DMAX 1000000001
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
int g, energCeruta, energie[1010], cost[1010];
int castig[3][5010];
void citire(){
in >> g >> energCeruta;
for(int i = 1; i <= g; i++){
in >> energie[i] >> cost[i];
}
in.close();
}
void intializare(){
for(int i = 0; i <= energCeruta; i++){
castig[1][i] = DMAX;
}
}
void rezolvare(){
for(int i = 1; i <= g; i++){
for(int j = 0; j <= energCeruta; j++) {
if(energie[i] >= j){
castig[2][j] = min(castig[1][j], cost[i]);
}else{
castig[2][j] = min(castig[1][j], castig[1][j - energie[i]] + cost[i]);
}
}
for(int j = 1; j <= energCeruta; j++){
castig[1][j] = castig[2][j];
}
}
if(castig[2][energCeruta] != DMAX){
out << castig[2][energCeruta];
}else{
out << -1;
}
}
int main() {
citire();
intializare();
rezolvare();
return 0;
}