Pagini recente » Cod sursa (job #1271748) | Cod sursa (job #3215732) | Cod sursa (job #2513543) | Cod sursa (job #65845) | Cod sursa (job #1895958)
#include<bits/stdc++.h>
#define oo 0x3f3f3f3f
using namespace std;
const int nmax = 1e3 + 5;
const int mmax = 1e5 + 5;
int val[nmax], cost[nmax];
int T[nmax][mmax];
int main()
{
int n, w;
FILE *in = fopen("energii.in", "r");
FILE *out = fopen("energii.out", "w");
//read
fscanf(in, "%d%d", &n, &w);
for(int i = 1; i <= n; i++)
fscanf(in, "%d%d", &cost[i], &val[i]);
//bordam
for(int i = 0; i <= n; i++)
for(int j = 1; j <= w; j++)
T[i][j] = oo;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= w; j ++)
{
if( cost[i] > j)
T[i][j] = min(T[i-1][j], val[i]);
else T[i][j] = min(T[i-1][j], T[i-1][j - cost[i]]);
}
}
/*
for(int i = 1; i <= n; i++){
for(int j = 0; j <= w; j++)
fprintf(out, "%d ", T[i][j]);
fprintf(out, "\n");
} */
int s = T[n][w];
if( s >= w)
fprintf(out, "%d", s);
else fprintf(out,"%d", -1);
return 0;
}