Cod sursa(job #2942252)

Utilizator samyro14Samy Dragos samyro14 Data 19 noiembrie 2022 14:11:50
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");
#define ll long long
const int maxn = 1e3;
const int maxe = 5e3;
int n;
int e_necesara;
struct generator{
    int e, cost;
}a[maxn + 2];
int dp[maxe + 4];
void read(){
    fin >> n >> e_necesara;
    for(int i = 1; i <= n; ++i) {
        fin >> a[i].e >> a[i].cost;
        if(a[i].e > e_necesara) a[i].e = e_necesara;
    }
}
void solve(){
    for(int i = 1; i <= e_necesara; ++i)
        dp[i] = 10000000;
    for(int i = 1; i <= n; ++i)
        for(int j = e_necesara - a[i].e; j >= 0; --j){
            if(dp[j + a[i].e] > dp[j] + a[i].cost)
                dp[j + a[i].e] = dp[j] + a[i].cost;
        }
    if(dp[e_necesara] != 10000000) fout << dp[e_necesara] << " ";
    else fout << "-1";
}
int main(){
    read();
    solve();
    return 0;
}
/*
    dp[i] costul minim pentru a produce energia i
*/