Cod sursa(job #2480016)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 24 octombrie 2019 19:18:49
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 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];
pair < int, int> type[N / 5];

bool cmp(pair < int, int > a, pair <int, int> b)
{
    return ((a.first > b.first) || (a.first == b.first && a.second < b.second));
}
int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);

    int n, m;

    fin >> n >> m;
    int mn = 10001;
    for(int i = 1; i <= n; ++i) fin >> type[i].first >> type[i].second;
    sort(type + 1, type + 1 + n, cmp);
    for(int i = 1; i <= n; ++i) {
        for(int j = 0; j <= type[i].second; ++j) {
            d[i][j] = d[i - 1][j];
            if(j >= type[i].second) d[i][j] = max(d[i][j], d[i - 1][j - type[i].second] + type[i].first);
            if(d[i][j] >= m) mn = min(mn, j);
        }
    }

    fout << ((mn == 10001) ? -1 : mn);
    return 0;
}