Cod sursa(job #362919)

Utilizator shnakoVlad Schnakovszki shnako Data 11 noiembrie 2009 12:29:07
Problema Energii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <stdio.h>
#include <stdlib.h>
FILE *f=fopen("energii.in", "r"), *g=fopen("energii.out", "w");
int aim, i, j, n, st[1005], cost, total, k;

typedef struct sethelo
{
	int e, c;
};

sethelo v[1005], aux;

void citeste(void)
{
	fscanf(f, "%d", &n);
	fscanf(f, "%d", &aim);
	for (i=1;i<=n;i++)
		fscanf(f, "%d%d", &v[i].e, &v[i].c);
	fclose(f);
}

void tipareste(int o)
{
	fprintf(g, "%d", o);
	fclose(g);
}

void sorteaza(void)
{
	for (i=1;i<=n;i++)
		for (j=i+1;j<=n;j++)
			if (v[i].c>v[j].c)
			{
				aux=v[i];
				v[i]=v[j];
				v[j]=aux;
			}
}

void alege(void)
{
	for (i=1;total<aim&&i<=n;i++)
	{
		cost+=v[i].c;
		total+=v[i].e;
	}
	if (total<aim)
	{
		tipareste(-1);
		exit(0);
	}
	k=i-1;
}

void costreduction(void)
{
	for (i=k;i>=1;i--)
		if (total-v[i].e>=aim)
			cost-=v[i].c;
}

int main(void)
{
	citeste();
	sorteaza();
	alege();
	costreduction();
	tipareste(cost);
	return 0;
}