what is the prob in this

Code:

#include<stdio.h>
int main()
{
int a[1000],b[1000],c[1000],new1[1000];
int temp=0,n,q,m,i,j,s,sum=0,flag=0,k,r,flag1=0,sum1=0,count=0,flag2=0;
//printf("enter the intergr");
scanf("%d",&n);
//printf("\n");
s=n;
for(i=0;i<n;i++)
{
scanf("%d",&c[i]);
}
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<=n-1;j++)
{
if ( c[i] > c[j] )
{
temp = c[i] ;
c[i] = c[j] ;
c[j] = temp ;
}
}
}
i=0;
q=0;
for(i=n-1;i>=0;i--)
{
a[q]=c[i];
//printf("a[q]%d\n",a[q]);
q++;
}
i=0;
j=0;
if(a[n-1]==0)
{
i=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("sum\n%d",sum);
r=sum%3;
//printf("r\n%d",r);
if(r==0)
{
flag=1;
m=i;
//printf("YES\n");
}
r=0;
else
{
for(j=n-1;j>=0;j--)
{
r=a[j]%3;
if((r==1)||(r==2))
{
sum=sum-a[j];
a[j]=0;
k=sum%3;
if(k==0)
break;
}
}
}
i=0;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
}
else
printf("-1");
return(0);
}