Cod sursa(job #3257846)

Utilizator mihail_11Ionescu Mihail mihail_11 Data 19 noiembrie 2024 17:47:04
Problema Energii Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
int t[1005][5005];
int gen[1005], cost[1005];
int main()
{
    int i,j,n,w;
    fin>>n>>w;
    for(i=1;i<=n;++i)
        fin>>gen[i]>>cost[i];
    ///t[i][j]=> cel mai mic cost pentru a avea cel putin j energie cu i generatoare
    for(i=1;i<=n;++i)
    {
        for(j=1;j<=w;++j)
        {
            if(j-gen[i]>0)
            {
                t[i][j]=min(t[i-1][j-gen[i]]+cost[i],t[i-1][j]);
                if(t[i][j]==0 && t[i-1][j-gen[i]]>0)
                {
                    t[i][j]=max(t[i-1][j-gen[i]]+cost[i],t[i-1][j]);
                }
            }
            else
            {
                t[i][j]=min(cost[i],t[i-1][j]);
                if(t[i][j]==0)
                {
                    t[i][j]=max(cost[i],t[i-1][j]);
                }
            }
        }
    }
    if(t[n][w]==0)
    {
        fout<<-1;
        return 0;
    }
    fout<<t[n][w];
    return 0;
}