Pagini recente » Cod sursa (job #819570) | Cod sursa (job #1354747) | Cod sursa (job #1194024) | Cod sursa (job #2439351) | Cod sursa (job #1310164)
#include <iostream>
#include <fstream>
#define inf (1<<30)
#define nmax 5005
using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");
int min(int x, int y)
{
if (x<y) return x;
return y;
}
int main()
{
int n,w,c,e,a[nmax],b[nmax];
int * first=a, *second=b, *aux;
f>>n>>w;
for(int i=1; i<=w; i++)
{
*(first+i)=inf;
}
for(int i=1; i<=n; i++)
{
f>>e>>c;
for(int j=1; j<=w; j++)
{
if (j-e>=1) *(second+j) = min(*(first+j-e)+c, *(first+j));
else *(second+j) = min(c, *(first+j));
}
aux=first;
first=second;
second=aux;
}
if (*(first+w)<inf) g<<*(first+w);
else g<<"-1";
return 0;
}