ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

No subject
Posted by ImranYusubov 27 Mar 2008 16:28
#include<iostream.h>
#include<math.h>

int i,j,a[35],n,max,min,max1,min1;

void main()
{

cin>>n;
 for(i=0;i<n;i++)
   cin>>a[i];

    for(i=0;i<n;i++)
     {max=a[i];
     for(j=i;j<n;j++)
    if(a[j]<max)
    {
    a[i]=a[j];
    a[j]=max;
    max=a[i];

    }
    }


  max=a[n-1];
  min=a[n-2];
  i=n-3;

  while(i>=0)
  {
   if(max<min)
    max=max+a[i];
    else
    min=min+a[i];
    i--;
       }


  max1=a[1];
  min1=a[0];
  i=2;

  while(i<n)
  {
   if(max1<min1)
    max1=max1+a[i];
    else
    min1=min1+a[i];
    i++;
       }

   if(abs(max-min)>abs(max1-min1))
     cout<<abs(max1-min1);
      else
      cout<<abs(max-min);
 }