#include<stdio.h>
int main()
{
int n,i,j,z,temp,k[50],a[50],atp,x;
while((scanf("%d",&n)!=EOF)&&n!=0)
{
for(i=0;i<n;i++)
{scanf("%d",&a[i]);}
for(i=0;i<n;i++)
{
z=0;
x=a[i];
while(x!=0)
{
z+=x%10;
x=x/10;
}
k[i]=z;
}
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
{
if(k[i]<k[j])
{
atp=k[i];
k[i]=k[j];
k[j]=atp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<n-1;i++)
{
printf("%d ",a[i]);
}
printf("%d\n",a[n-1]);
}
return 0;
}
int main()
{
int n,i,j,z,temp,k[50],a[50],atp,x;
while((scanf("%d",&n)!=EOF)&&n!=0)
{
for(i=0;i<n;i++)
{scanf("%d",&a[i]);}
for(i=0;i<n;i++)
{
z=0;
x=a[i];
while(x!=0)
{
z+=x%10;
x=x/10;
}
k[i]=z;
}
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
{
if(k[i]<k[j])
{
atp=k[i];
k[i]=k[j];
k[j]=atp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<n-1;i++)
{
printf("%d ",a[i]);
}
printf("%d\n",a[n-1]);
}
return 0;
}