Cod sursa(job #2480482)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 25 octombrie 2019 17:34:44
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 5005, limit = 2e9;

int power[N / 5], cost[N / 5], d[N / 5][N];

int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);

    int n, m, mn = -1;

    fin >> n >> m;
    for(int i = 1; i <= n; ++i) fin >> power[i] >> cost[i];

    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            d[i][j] = limit;
        }
    }

    d[1][power[1]] = cost[1];

    for(int i = 2; i <= n; ++i) {
        for(int j = 0; j <= m; ++j) {
            if(j >= power[i]) {
                d[i][j] = min(d[i - 1][j], d[i - 1][j - power[i]] + cost[i]);
            }
            else {
                d[i][j] = min(d[i - 1][j], cost[i]);
            }
        }
    }

    fout << ((d[n][m] != limit) ? d[n][m] : -1);
    return 0;
}