Pagini recente » Cod sursa (job #1381990) | Cod sursa (job #1858953) | Istoria paginii runda/pregatire_oji_11-12_4 | Cod sursa (job #351560) | Cod sursa (job #2532624)
#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[N][M], amount[N], cost[N];
int main() {
in >> n >> total_amount;
for (int i = 1; i <= n; i++)
in >> amount[i] >> cost[i];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= total_amount; j++)
dp[i][j] = 5 * 1e8;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= total_amount; j++) {
if (amount[i] < j) dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - amount[i]] + cost[i]);
else
dp[i][j] = min(dp[i - 1][j], cost[i]);
}
}
if (dp[n][total_amount] == 5 * 1e8) out << "-1";
else
out << dp[n][total_amount];
return 0;
}