Cod sursa(job #2981663)

Utilizator raresgherasaRares Gherasa raresgherasa Data 18 februarie 2023 14:29:51
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;

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

const int kN = 1e4 + 5;

int dp[kN];
int n, W;

int main(){
   ios_base::sync_with_stdio(false);

   fin >> n >> W;
   fill(dp, dp + kN, -1);
   dp[0] = 0;
   for (int i = 1; i <= n; i++){
      int e, c; fin >> e >> c;
      for (int j = W; j >= 0; j--){
         if (j >= e){
            if (dp[j - e] == -1){
               continue;
            }
            if (dp[j] == -1){
               dp[j] = dp[j - e] + c;
            }
            else{
               dp[j] = min(dp[j], dp[j - e] + c);
            }
         }
         else{
            dp[j] = ((dp[j] == -1) ? c : min(dp[j], c));
         }
      }
   }
   fout << dp[W];
}