Cod sursa(job #2733722)

Utilizator Sho10Andrei Alexandru Sho10 Data 30 martie 2021 19:36:53
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 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[50005];
pair<ll,ll>a[1005];
bool cmp(pair<ll,ll>a,pair<ll,ll>b){
if(a.f==b.f){
    return a.s<b.s;
}else {
return a.f>b.f;
}
}
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].f>>a[i].s;
}
sort(a+1,a+n+1,cmp);
for(ll i=1;i<=w;i++)
{
    dp[i]=LINF;
}
for(ll i=1;i<=n;i++)
{
    for(ll j=w;j>=0;j--)
    {
        ll x=j+a[i].f;
        x=min(x,w);
        dp[x]=min(dp[x],dp[j]+a[i].s);
    }
}
if(dp[w]==LINF){
    cout<<"-1"<<endl;
}else {
cout<<dp[w]<<endl;
}
}