Pagini recente » Cod sursa (job #193489) | Cod sursa (job #2132776) | Cod sursa (job #2216031) | Cod sursa (job #2357413) | Cod sursa (job #2440748)
#include <iostream>
#include <fstream>
#define MaxG 1002
#define MaxW 5002
#define undefined -1
using namespace std;
ifstream f("energii.in");
ofstream fout("energii.out");
typedef long long ll;
int g,w;
ll e[MaxG],c[MaxW];
ll m[MaxG][MaxW];
bool sol=false;
ll rez(int g,int w)
{
if(w<=0)
{
sol=true;
return 0;
}
if(g==0)
return 100000000;
if(m[g][w]!= undefined) return m[g][w];
ll rez1 = rez(g-1,w);
ll rez2 = rez(g-1,w-e[g])+c[g];
m[g][w]=min(rez1,rez2);
return m[g][w];
}
int main()
{
f>>g>>w;
for(int i=1;i<=g;i++)
f>>e[i]>>c[i];
for(int i=1;i<=g;i++)
for(int j=1;j<=w;j++)
m[i][j]=undefined;
ll solutie = rez(g,w);
fout<<(sol? solutie : -1);
f.close();
fout.close();
return 0;
}