chef (simple and fun)



Chef has n columns of ground. Each column has it's height . Chef can choose any column and increase its height by 1 using 1 cube of ground.
Chef wants to spend exactly m(neither less nor more) cubes. Can he make this in such a way that the heights of all columns become equal?
 
Read n,m
read heights of all 'n' columns
If Chef can spend all cubes and make the columns equal print Yes else print No.
Program:
 
#include<stdio.h>
void sort(int *s,int  p)
{
           int temp,i,j;
           for(i=0;i<p;i++)
           {
                    for(j=i+1;j<p;j++)
                           if((*(s+i))>(*(s+j)))
                           {
                                   temp=*(s+i);
                                   (*(s+i))=(*(s+j));
                                   *(s+j)=temp;
                           }
             }
}
int main()
{
              int n,m,i,j;
              printf("n, m: ");
              scanf("%d%d",&n,&m);                                /* read n,m */
              int a[n];
              printf("heights of all n columns: ");
              for(i=0;i<n;i++)                                              /* read heights of all n columns */
              scanf("%d",&a[i]);
              sort(a,n);                                                       /* sort the heights in incresing order */
              for(i=n-1;i>0;i--)    /* make all the column heights to the heighest height among all */
              {
                       j=i-1;
                       while((a[i])>(a[j]))
                       {
                                    a[j]++;
                                    m--;
                       }
              }
              if(m==0)                                            /* if m=0 i.e.,exactly m cubes are spent */
                      printf("Yes.");
              else printf("No.");
              return 0;
} 
sample input and outputs:
n, m: 5 8
heights of all n columns: 3 3 4 2 1
No.
n, m: 5 7

heights of all n columns: 3 3 4 2 1
Yes.