Pagini recente » Istoria paginii utilizator/alexprostean | Istoria paginii utilizator/bogdan1408 | Istoria paginii utilizator/jozsue | Monitorul de evaluare | Cod sursa (job #2008043)
#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, a;
int main () {
fin >> n >> w;
for (i = 1; i <= n; i++)
fin >> e[i] >> c[i];
x = 1; y = 2;
enrg[1][e[1]] = c[1];
for (i = 2; i <= n; i++)
{
for (j = 1; j <= w+e[i]; j++)
{
if (enrg[x][j] != 0 && j <= w) enrg[y][j] = enrg[x][j];
if (enrg[x][j-e[i]] != 0 && j > e[i] && (enrg[y][j] == 0 || enrg[y][j] > enrg[x][j-e[i]]+c[i]))
enrg[y][j] = enrg[x][j-e[i]]+c[i];
if (j >= w && (enrg[y][j] < sol || sol == 0) && enrg[y][j] != 0)
sol = enrg[y][j];
if (j > w) enrg[y][j] = 0;
}
if (c[i] < enrg[y][e[i]] || enrg[y][e[i]] == 0) enrg[y][e[i]] = c[i];
if (e[i] >= w && (c[i] < sol || sol == 0))
sol = c[i];
a = x;
x = y;
y = a;
}
if (sol != 0) fout << sol << "\n";
else fout << -1 << "\n";
}