Cod sursa(job #2942260)

Utilizator samyro14Samy Dragos samyro14 Data 19 noiembrie 2022 14:28:36
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 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(){
    int ans = 10000000;
    for(int i = 1; i <= e_necesara; ++i)
        dp[i] = 10000000;
    for(int i = 1; i <= n; ++i)
        for(int j = e_necesara; j >= 0; --j){
            if(j + a[i].e > e_necesara && dp[e_necesara] > dp[j] + a[i].cost){
                dp[e_necesara] = dp[j] + a[i].cost;
                ans = min(ans, dp[e_necesara]);
            }
            else if(j + a[i].e <= e_necesara && dp[j + a[i].e] > dp[j] + a[i].cost){
                dp[j + a[i].e] = dp[j] + a[i].cost;
                if(j + a[i].e == e_necesara)
                    ans = min(ans, dp[j + a[i].e]);
            }

        }
    if(ans != 10000000) fout << ans << " ";
    else fout << "-1";
}
int main(){
    read();
    solve();
    return 0;
}
/*
    dp[i] costul minim pentru a produce energia i
*/