Pagini recente » Cod sursa (job #2729192) | Cod sursa (job #2402661) | Cod sursa (job #2479808) | Cod sursa (job #48049) | Cod sursa (job #2116966)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
const int GMAX = 1005;
const int WMAX = 5005;
const int oo = 20022002;
int G, W;
vector <pair<int, int> > V;
int DP[GMAX][WMAX];
void Read(){
in >> G >> W;
for(int i = 1; i <= G; ++i){
int energy, cost;
in >> energy >> cost;
V.push_back(make_pair(energy, cost));
}
for(int i = 0; i <= W; ++i)
DP[0][i] = oo;
}
void SolveAndPrint(){
for(int i = 1; i <= G; ++i){
for(int energy = 1; energy <= W; ++energy){
int generator_energy = V[i - 1].first;
int generator_cost = V[i - 1].second;
DP[i][energy] = DP[i - 1][energy];
if(generator_energy >= energy)
DP[i][energy] = min(DP[i][energy], generator_cost);
else
DP[i][energy] = min(DP[i][energy], generator_cost
+ DP[i - 1][energy - generator_energy]);
}
}
out << (DP[G][W] != oo ? DP[G][W] : -1) << "\n";
}
int main(){
Read();
SolveAndPrint();
}