Cod sursa(job #2733715)

Utilizator Sho10Andrei Alexandru Sho10 Data 30 martie 2021 19:28:18
Problema Energii Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define aint(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,w,dp[100005];
pair<ll,ll>a[1005];
int32_t main(){
CODE_START;
ifstream cin("energii.in");
ofstream cout("energii.out");
cin>>n>>w;
for(ll i=1;i<=n;i++)
{
    cin>>a[i].s>>a[i].f;
}
dp[0]=0;
for(ll i=1;i<=1e5;i++)
{
    dp[i]=LINF;
}
for(ll i=1;i<=n;i++)
{
for(ll j=w;j>=0;j--)
{
    if(j==0){
    dp[min(w,j+a[i].s)]=min(dp[min(w,j+a[i].s)],dp[j]+a[i].f);
    }else if(dp[j]!=0){
    dp[min(w,j+a[i].s)]=min(dp[min(w,j+a[i].s)],dp[j]+a[i].f);
    }
}
}
ll ans=LINF;
for(ll i=1e5;i>=w;i--)
{
    ans=min(ans,dp[i]);
}
if(ans==LINF){
    ans=-1;
}
cout<<ans<<endl;
}