Mai intai trebuie sa te autentifici.
Cod sursa(job #1903002)
| Utilizator | Data | 4 martie 2017 22:02:57 | |
|---|---|---|---|
| Problema | Energii | Scor | 5 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 1.03 kb |
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("energii.in");
ofstream fout("energii.out");
struct energie{
int cant,cost;
}a[10001];
int n,i,j,g,w,s,m,stot,costt;
bool cmp(energie a, energie b)
{
if(a.cant!=b.cant)
return a.cant<b.cant;
else
return a.cost<b.cost;
}
int main()
{
f>>g>>w;
for(i=1;i<=g;i++)
{f>>a[i].cant>>a[i].cost;
stot+=a[i].cant;
costt+=a[i].cost;
}
if(stot<w)
fout<<"-1";
else
if(stot==w)
fout<<costt;
else
{
m=g;
sort(a+1,a+g+1,cmp);
while(a[g].cant<=w)
{ w-=a[g].cant;
g--;
}
for(i=g+1;i<=m;i++)
s+=a[i].cost;
if(w!=0)
{i=1;
while(w>=0)
{
w-=a[i].cant;
i++;
}
for(j=1;j<=i-1;j++)
s+=a[j].cost;
}
fout<<s;
}
return 0;
}
