Cod sursa(job #696464)

Utilizator dudutCancel Radu Constantin dudut Data 28 februarie 2012 18:38:24
Problema Energii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include<iostream>
#include<stdio.h>
using namespace std;

struct asfg{ int e;
int c;
};
asfg v[1002];
int i,j,eg,n,k,s,aux;

int main()
{freopen("energii.in","r",stdin);
freopen("energii.out","w",stdout);
scanf("%d%d", &n, &eg);
for(i=1;i<=n;i++)
	{scanf("%d%d", &v[i].e, &v[i].c);
s+=v[i].e;}
if(s<eg)
	printf("-1");
else
	if(s==eg)
	{s=0;
	for(i=1;i<=n;i++)
		s+=v[i].c;
	printf("%d",s);
	}

	else
	{s=0;
			for(j=1;j<=n;j++)
				{for(k=j;k<=n;k++)
				if(v[j].e-v[j].c<v[k].e-v[k].c)
				{aux=v[j].e;
				v[j].e=v[k].e;
				v[k].e=aux;
				aux=v[j].c;
				v[j].c=v[k].c;
				v[k].c=aux;}
				else
					if(v[j].e-v[j].c==v[k].e-v[k].c)
						if(v[j].e<v[k].e)
							{aux=v[j].e;
							v[j].e=v[k].e;
							v[k].e=aux;
							aux=v[j].c;
				v[j].c=v[k].c;
				v[k].c=aux;}
				s+=v[j].c;
				eg-=v[j].e;
				if(eg<=0)
				{printf("%d",s);j=n+1;
				}
				}

}
}