Pagini recente » Cod sursa (job #3171568) | Cod sursa (job #2035720) | Cod sursa (job #1421470) | Cod sursa (job #1377545) | Cod sursa (job #2480482)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("energii.in");
ofstream fout ("energii.out");
const int N = 5005, limit = 2e9;
int power[N / 5], cost[N / 5], d[N / 5][N];
int main()
{
ios::sync_with_stdio(false);
fin.tie(0);
int n, m, mn = -1;
fin >> n >> m;
for(int i = 1; i <= n; ++i) fin >> power[i] >> cost[i];
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
d[i][j] = limit;
}
}
d[1][power[1]] = cost[1];
for(int i = 2; i <= n; ++i) {
for(int j = 0; j <= m; ++j) {
if(j >= power[i]) {
d[i][j] = min(d[i - 1][j], d[i - 1][j - power[i]] + cost[i]);
}
else {
d[i][j] = min(d[i - 1][j], cost[i]);
}
}
}
fout << ((d[n][m] != limit) ? d[n][m] : -1);
return 0;
}