Cod sursa(job #2932320)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 2 noiembrie 2022 17:43:24
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>

using namespace std;

ifstream cin("energii.in");
ofstream cout("energii.out");

const int EMAX = 1e7;
const int NMAX = 1e3;

int n, m, s;
pair <int, int> dp[NMAX + 1], v[NMAX + 1];

int main(){

    cin >> n >> m;

    for(int i = 1; i <= n; i++){

        cin >> v[i].first >> v[i].second;
        dp[i] = v[i];

    }

    for(int i = 2; i <= n; i++){

        dp[i] = dp[i - 1];

        for(int j = 1; j < i; j++){

            pair <int, int> temp;
            temp.first = dp[j].first + v[i].first; // energia
            temp.second = dp[j].second + v[i].second; // costul

            if(temp.first >= m){

                // am o energie produsa mai mare sau egala cu cea necesara, deci ma intereseaza costul minim
                if(dp[i].second > temp.second)
                    dp[i] = temp;

            }else{

                // am o energie produsa mai mica cu cea necesara, deci ma intereseaza o energie cat mai mare cu un cost cat mai mic

                if(dp[i].first < temp.first || (dp[i].first == temp.first && dp[i].second > temp.second))
                    dp[i] = temp;

            }

        }

    }

    int ans = 2e9;

    for(int i = 1; i <= n; i++)
        if(dp[i].first >= m)
            ans = min(ans, dp[i].second);

    cout << ans;

    return 0;
}