Cod sursa(job #2485047)

Utilizator buhaidarius@gmail.comBuhai Darius [email protected] Data 31 octombrie 2019 21:57:48
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <iostream>
#include <fstream>

#define MAXL 5007

using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");

int dp[MAXL];

int main() {
    int n, w, x, y, z, pos, mine = -1;
    fin>>n>>w;
    for(int i=0;i<=w;i++)
        dp[i] = -1;
    for(int i=0;i<n;i++){
        fin>>x>>y;
        for(int j=w;j>=0;j--)
            if(dp[j]!=-1){
                z = dp[j]+y;
                pos = x+j;
                if(pos>=w && (mine==-1 || z<mine))
                    mine = z;
                else if(pos<w && (dp[pos]==-1 || z<dp[pos]))
                    dp[pos] = z;
            }
        if(x>=w && (mine==-1 || y<mine))
            mine = y;
        else if(x<w && (dp[x]==-1 || y<dp[x]))
            dp[x] = y;
    }
    fout<<mine;
    return 0;
}