Cod sursa(job #2532294)

Utilizator memecoinMeme Coin memecoin Data 27 ianuarie 2020 18:04:18
Problema Energii Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#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 0x3f3f3f3f3f3f3f3f

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;
}