Cod sursa(job #2479989)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 24 octombrie 2019 18:45:22
Problema Energii Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 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 + 5], cost[N / 5 + 5], d[N * 2 - 5][N];

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

    int n, m;

    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 = 0; j <= 10000; ++j) {
            d[i][j] = d[i - 1][j];
            if(j >= cost[i]) d[i][j] = max(d[i][j], d[i - 1][j - cost[i]] + power[i]);
        }
    }

    for(int i = 0; i <= 10000; ++i) {
        if(d[n][i] >= m) {
            fout << i << "\n";
            return 0;
        }
    }
    fout << "-1";
    return 0;
}