Cod sursa(job #1334173)

Utilizator GinguIonutGinguIonut GinguIonut Data 4 februarie 2015 00:03:30
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
int cant[1001],cost[1001],v[25002500],i,j,n,w,costmax,sol,ok,st,dr,mijl;
int main()
{
    fin>>n>>w;
    for(i=1;i<=n;i++)
    {
        fin>>cant[i]>>cost[i];
        costmax+=cost[i];
    }
    for(i=1;i<=n;i++)
    {
        for(j=costmax-cost[i];j>=0;j--)
        {
            if(v[j]+cant[i]>v[j+cost[i]])
                v[j+cost[i]]=v[j]+cant[i];
        }
    }
    st=1;
    dr=costmax;
    while(st<=dr)
    {
        mijl=(st+dr)/2;
        if(v[mijl]>=w)
            if(v[mijl]==w)
            {
                sol=mijl;
                ok=1;
                dr=mijl-1;
            }
            else
                dr=mijl-1;
        else
            st=mijl+1;
    }
    if(ok==1)
        fout<<sol;
    else
        fout<<-1;
 return 0;
}