Cod sursa(job #2849774)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 15 februarie 2022 19:14:20
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <bits/stdc++.h>
#define GMAX 1005
#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 energy[GMAX], cost[GMAX];
int dp[GMAX];

/*******************************/
/// FUNCTIONS

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

    for (int i = 1 ; i <= G ; ++ i)
    {
        f >> energy[i] >> cost[i];
    }
}
///-------------------------------------
inline void Solution()
{
    memset(dp, INF, sizeof(dp));
    dp[0] = 0;
    for (int i = 1 ; i <= G ; ++ i)
    {
        for (int val = 1 ; val <= W ; ++ val)
        {
            if (val <= energy[i])
            {
                dp[val] = min(dp[val], cost[i]);
            }
            else
            {
                dp[val] = min(dp[val], dp[val - energy[i]] + cost[i]);
            }
        }
    }

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