Pagini recente » Cod sursa (job #460431) | Clasament dupa rating | Cod sursa (job #2189739) | Cod sursa (job #259564) | Cod sursa (job #2008198)
#include <iostream>
#include <fstream>
#define E 20003
#define dim 1003
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
int n, i, j, w, e[dim], c[dim], enrg[3][E], x, y, sol = 60000000, a;
bool bn[3][E];
int main () {
fin >> n >> w;
for (i = 1; i <= n; i++)
fin >> e[i] >> c[i];
x = 1; y = 2;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= w+e[i]; j++)
{
if (bn[x][j] == 1)
{
enrg[y][j] = enrg[x][j];
bn[y][j] = 1;
}
if ((bn[x][j-e[i]] == 1 || j <= e[i]) && (bn[y][j] == 0 || enrg[y][j] > enrg[x][j-e[i]]+c[i]))
{
enrg[y][j] = enrg[x][j-e[i]]+c[i];
bn[y][j] = 1;
}
if (j >= w && enrg[y][j] < sol && bn[y][j] == 1)
sol = enrg[y][j];
bn[x][j-e[i]] = 0;
}
if (c[i] < enrg[y][e[i]] || bn[y][e[i]] == 0)
{
enrg[y][e[i]] = c[i];
bn[y][e[i]] = 1;
}
if (e[i] >= w && c[i] < sol)
sol = c[i];
a = x;
x = y;
y = a;
}
if (sol != 60000000) fout << sol << "\n";
else fout << -1 << "\n";
}