View Code of Problem 2886

#include<stdio.h>
#include<stdlib.h>
void Insert(int *a,int len,int Count)
{
	int i,biger,count;
	count = Count;
	a[count] = len;
	while(count != 0)
	{
		i = (count-1)/2;
		if(a[count] > a[i])
		{
			biger = a[count];
			a[count] = a[i];
			a[i] = biger;
			count = i;
		}
		else
			break;
	}
}

int DeleteMax(int *a,int Count)
{
	int count = Count;
	int i = 0,smaller,j,k,t;
	t = a[0];
	a[0] = a[count --];
	while(i != count)
	{
		j = 2 * i + 1;
		if(j + 1 <= count)
		{
			if(a[i] > a[j]&&a[i]>a[j+1])
				i = count;
			else if(a[j + 1] >= a[j] && a[j+1] > a[i])
			{
				smaller = a[i];
				a[i] = a[j + 1];
				a[j + 1] = smaller;
				i = j + 1;
			}
			else if(a[j] > a[j+1]&&a[j] > a[i])
			{
				smaller = a[i];
				a[i] = a[j];
				a[j] = smaller;
				i = j;
			}
		}
		else if(j > count)
		{
			break;
		}
		else
		{
			if(a[i] < a[j])
			{
				smaller = a[i];
				a[i] = a[j];
				a[j] = smaller;
				i = j;
			}
			else
				i = count;
		}
	}
	return t;
}
int main(void)
{
	int N,sum = 0,l,i = 0,count = 0,S = 0;
	int t1;
	int len[20000];
	scanf("%d",&N);
	while(count < N)
	{
		scanf("%d",&l);
		Insert(len,l,count);
		count ++;
		sum += l;
	}
	count --;
//	printf("N = %d,count = %d\n",N,count);
//	for(i = 0;i <= count;i ++)
//		printf("%d ",len[i]);
//	printf("sum  = %d\n",sum);
	while(count > 0)
	{
		S += sum;
		t1 = DeleteMax(len,count);
		sum -= t1;
		printf("t1 = %d\n",t1);
		count --;
	}
	printf("%d\n",S);

	return 0;
}

Double click to view unformatted code.


Back to problem 2886