Pagini recente » Cod sursa (job #2489297) | Cod sursa (job #2532293)
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
using namespace std;
#ifdef DEBUG
string name = "data";
#else
string name = "energii";
#endif
ifstream fin(name + ".in");
ofstream fout(name + ".out");
#define MAXN 1024
#define MAXW 5001
int64_t n, w;
int64_t v[MAXN];
int64_t c[MAXN];
int64_t best[MAXW];
int64_t p[MAXW];
int main() {
fin >> n >> w;
memset(best, 0x3f, sizeof(best));
memset(p, 0x3f, sizeof(p));
for (int i = 0 ; i < n ; ++i) {
fin >> v[i];
fin >> c[i];
}
for (int i = 0; i < n; ++i) {
int64_t key = min(w, v[i]);
if (c[i] < best[key]) {
best[key] = c[i];
p[key] = i;
}
for (int j = 0; j <= w; ++j) {
if (best[j] != INF && p[j] != i) {
int64_t key = min(w, j + v[i]);
if (best[j] + c[i] < best[key]) {
best[key] = best[j] + c[i];
p[key] = i;
}
}
}
}
if (best[w] != INF) {
fout << best[w];
} else {
fout << -1;
}
return 0;
}