Cod sursa(job #3265378)

Utilizator luc3lexa_Alexandrescu Luca luc3lexa_ Data 29 decembrie 2024 19:40:27
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");

const int nmax = 5010;
const int inf = 1e9;

vector<int> dp(nmax,inf);
vector<pair<int,int>> values(nmax,{0,0});
int n,w;

void read_input(){
	fin >> n >> w;
	for(int i = 1; i <=n; i++){
		fin >> values[i].first >> values[i].second;
	}
}

void solve(){
	dp[0] = 0;
	
	for(int i = 1; i <=n; i++){
		for(int j = w; j >= 0; j--){
			if(dp[j] != inf){
				int weight = j + values[i].first;
				if(weight > w){weight = w;}
				dp[weight] = min(dp[weight],dp[j] + values[i].second);
			}		
		}
	};
	
	fout << (dp[w] == inf ? -1 : dp[w]);
}

int main(){
	read_input();
	solve();
	return 0;
}