Cod sursa(job #2981670)

Utilizator domistnSatnoianu Dominic Ioan domistn Data 18 februarie 2023 14:51:56
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <iostream>

using namespace std;

const int NMAX = 1000, WMAX = 5000;

int n, w, dp[WMAX + 1];
//dp[i] = costul minim pentru generarea energiei i

int main()
{
    freopen("energii.in", "r", stdin);
    freopen("energii.out", "w", stdout);
    scanf("%d%d", &n, &w);
    for(int i = 0; i <= w; ++i)
        dp[i] = 1e9;
    for(int i = 1, energ, cost; i <= n; ++i) {
        scanf("%d%d", &energ, &cost);
        for(int j = 1; j <= w; ++j) {
            if(j <= energ) dp[j] = min(dp[j], cost);
            else dp[j] = min(dp[j], dp[j - energ] + cost);
        }
    }
    printf("%d", dp[w] == 1e9 ? -1 : dp[w]);
    return 0;
}