Cod sursa(job #3263941)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 17 decembrie 2024 10:52:18
Problema Energii Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("energii.in");
ofstream g ("energii.out");

const int NMAX = 1e3;
const int WMAX = 5e3;
const int VMAX = 1e9;

int dp[2][WMAX + 1];

int main()
{
    
    for(int i=0; i<2; i++)
        for(int j=0; j<=WMAX; j++)
            dp[i][j] = VMAX;
            
    int n, w;
    f >> n >> w;
    
    dp[0][0] = 0;
    
    int costmin = 2e9;
    
    for(int i=1; i<=n; i++){
        
        int x, y;
        
        f >> x >> y;
        
        for(int j=0; j<w; j++){
            
            if(dp[(i + 1) % 2][j] != 2e9){
                
                int cost = dp[(i + 1) % 2][j] + y;
                int pret = j + x;
                
                if(pret >= w)
                    costmin = min(costmin, cost);
                    
                else{
                    
                    dp[i % 2][j + x] = min(dp[(i + 1) % 2][j + x], dp[(i + 1) % 2][j] + y);
                    
                }
                
            }
                
            
        }
        
    }
    
    if(costmin == 2e9)
        g << -1;
    
    else g << costmin;

    return 0;
}