Pagini recente » Cod sursa (job #584320) | Cod sursa (job #2907646) | Cod sursa (job #2926483) | Cod sursa (job #2484727) | Cod sursa (job #2532620)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
const int N = 5002;
const long long M = 10002;
int n, total_amount;
long long dp[M][M], amount[N], cost[N];
int main() {
in >> n >> total_amount;
for (int i = 1; i <= n; i++)
in >> amount[i] >> cost[i];
int l = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= total_amount; j++)
dp[i][j] = 5 * 1e7;
for (int i = 1; i <= n; i++, l = 1 - l) {
for (int j = 1; j <= total_amount; j++) {
if (amount[i] < j) dp[1 - l][j] = min(dp[1 - l][j], dp[l][j - amount[i]] + cost[i]);
else
dp[1 - l][j] = min(dp[l][j], cost[i]);
}
}
if (dp[l][total_amount] == 5 * 1e7) out << "-1";
else
out << dp[l][total_amount];
return 0;
}