Pagini recente » Cod sursa (job #2901387) | Cod sursa (job #1807658) | Cod sursa (job #2451886) | Cod sursa (job #12996) | Cod sursa (job #2532618)
#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] = 10002;
for (int i = 1; i <= n; i++, l = 1 - l) {
for (int j = 0; 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]);
}
}
out << dp[l][total_amount];
return 0;
}
/*#include <iostream>
#include <algorithm>
using namespace std;
struct object {
int value, weight;
}v[1001];
bool cmp(const object& x, const object& y) {
return (x.value * y.weight > y.value* x.weight);
}
int main() {
int maximumWeight, n;
cin >> n >> maximumWeight;
for (int i = 1; i <= n; i++) {
cin >> v[i].weight >> v[i].value;
}
sort(v + 1, v + n + 1, cmp);
for (int i = 1; i <= n; i++)
cout << v[i].weight << ' ' << v[i].value << "; ";
cout << endl;
int i = 0;
double sum = 0;
while (i <= n && maximumWeight > 0) {
if (v[i].weight <= maximumWeight) {
sum += v[i].value;
maximumWeight -= v[i].weight;
}
else {
sum += (double)maximumWeight / v[i].weight * v[i].value;
maximumWeight = 0;
}
i++;
}
cout << sum;
return 0;
}*/