Cod sursa(job #2517422)

Utilizator mirceatlxhaha haha mirceatlx Data 3 ianuarie 2020 15:56:08
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#define NMAX 1005
#define NGMAX 1e9
using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");

int G, W;
vector < pair <int , int> > arr(NMAX);
int dp[100000005];

int main()
{
    int ene, cost, maxx = 0;
    fin >> G >> W;
    for(int i = 1; i <= G; i++){
        fin >> ene >> cost;
        arr[i].first = ene;
        arr[i].second = cost;
        maxx += ene;
    }
    if(maxx < W){
        fout << -1 << "\n";
        return 0;
    }
    for(int i = 1; i <= maxx; i++){
        dp[i] = 1e9;
    }
    int k;
    for(int i = 1; i <= G; i++){
        for(int j = W; j >= 0; j--){
            k = j + arr[i].first;
            k = min(k, W);
            dp[k] = min(dp[k],dp[j] + arr[i].second);
        }
    }
    fout << dp[W] << "\n";

    return 0;
}