Cod sursa(job #2849764)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 15 februarie 2022 19:00:49
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <bits/stdc++.h>
#define WMAX 5005
#define INF 1000000005

using namespace std;

/*******************************/
// INPUT / OUTPUT
ifstream f("energii.in");
ofstream g("energii.out");
/*******************************/
/// GLOBAL DECLARATIONS

int G, W;
int ans;
int dp[WMAX]; // costul minim pentru a face i energie

vector <pair <int, int> > generators;
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> G >> W;

    int energy, cost;
    for (int i = 0 ; i < G ; ++ i)
    {
        f >> energy >> cost;
        generators.push_back({energy, cost});
    }
}
///-------------------------------------
inline void Solution()
{
    memset(dp, INF, sizeof(dp));
    dp[0] = 0;
    for (int energy = 1 ; energy <= W ; ++ energy)
    {
        for (auto generator: generators)
        {
            if (generator.first <= energy)
            {
                dp[energy] = min(dp[energy], dp[energy - generator.first] + generator.second);
            }        
        }
    }

    if (dp[W] != INF) ans = dp[W];
    else ans = -1;
}
///-------------------------------------
inline void Output()
{
    g << ans;
}
///-------------------------------------
int main()
{
    ReadInput();
    Solution();
    Output();
    return 0;
}