Cod sursa(job #2637793)

Utilizator alex.prohnitchiAlex Prohnitchi alex.prohnitchi Data 24 iulie 2020 22:36:05
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

typedef long long ll;

const ll mod=1e9+7;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll inf=2e9;
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define rc(x)  return cout<<x<<"\n",0
#define sz(s)  (int) s.size()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define PI 3.14159265358979

using namespace std;

ll t,dp[3][5005];

int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ifstream cin("energii.in");
	ofstream cout("energii.out");
	ll n,w;
	cin >> n >> w;
	for (int i=1; i<=w; i++) {
		dp[0][i]=inf;
	}
	for (int i=1; i<=n; i++) {
		ll e_p,cost;
		cin >> e_p >> cost;
		for (int j=1; j<=w; j++) {
			if (j-e_p<0) {
				dp[1][j]=min(dp[0][j],cost);
			}
			else {
				dp[1][j]=min(dp[0][j],dp[0][j-e_p]+cost);
			}
		}
		for (int i=1; i<=w; i++) {
			dp[0][i]=dp[1][i];
		}
	}
	if (dp[1][w]==inf) cout << -1 << '\n';
	else cout << dp[1][w] << '\n';
}