Pagini recente » Cod sursa (job #1410457) | Cod sursa (job #806136) | Cod sursa (job #1445013) | Cod sursa (job #435336) | Cod sursa (job #2342096)
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
const int N=1002;
int n,gmax,grmax;
struct obiect{
int pret;
int g;
float raport;
};
obiect a[N];
bool cmp(obiect x,obiect y)
{
return x.raport<=y.raport;
}
void citire()
{
fin>>n;
fin>>gmax;
for(int i=1;i<=n;i++)
{
fin>>a[i].g>>a[i].pret;
a[i].raport=(float)a[i].pret/a[i].g;
grmax+=a[i].g;
}
sort(a+1,a+n+1,cmp);
}
void afisare()
{
for(int i=1;i<=n;i++)
{
fout<<a[i].g<<" "<<a[i].pret<<" "<<a[i].raport<<'\n';
}
}
int main()
{
int i,j;
long long sc=0,gc=0,smax=1999999;
citire();
for(i=1;i<=n;i++)
{
if(gc>=gmax)
{
if(sc<=smax)
smax=sc;
while(gc<gmax)
{
sc-=a[i].pret;
gc-=a[i].g;
}
}
else
{
sc+=a[i].pret;
gc+=a[i].g;
}
}
if(grmax<gmax)
fout<<-1;
else fout<<smax;
return 0;
}