program ideone;
uses Math;
Const smax = 99;
Type simple = array[0..smax] of smallint;
Var data, tempData:simple; i, n:smallint;
procedure writeArr(arr:simple);
Var i:smallint;
begin
for i:=0 to n-1 do write(arr[i], ' ');
writeln();
end;
function sumOfDigitsInNum(num:smallint):smallint;
Var i, t:smallint;
begin
t:=0;
for i:=0 to 5 do t := t + ((abs(num) div floor(power(10, i))) -
10*(abs(num) div floor(power(10, i+1))));
sumOfDigitsInNum:=t;
end;
procedure swap(Var a, b:smallint);
Var t:smallint;
begin
t:=a;
a:=b;
b:=t;
end;
function customSort(data, tempData:simple):simple;
Var i, j, t:smallint;
begin
for i:=0 to n-1 do begin
for j:=0 to n-2 do begin
if tempData[j]>tempData[j+1] then begin
swap(tempData[j], tempData[j+1]);
swap(data[j], data[j+1]);
end;
if (tempData[j]=tempData[j+1]) and (data[j]>data[j+1]) then begin
swap(tempData[j], tempData[j+1]);
swap(data[j], data[j+1]);
end;
end;
end;
customSort:=data;
end;
begin
read(n);
for i:=0 to n-1 do read(data[i]);
for i:=0 to n-1 do tempData[i]:=sumOfDigitsInNum(data[i]);
writeArr(customSort(data, tempData));
end.
cHJvZ3JhbSBpZGVvbmU7Cgp1c2VzIE1hdGg7CgpDb25zdCBzbWF4ID0gOTk7ClR5cGUgc2ltcGxlID0gYXJyYXlbMC4uc21heF0gb2Ygc21hbGxpbnQ7ClZhciBkYXRhLCB0ZW1wRGF0YTpzaW1wbGU7IGksIG46c21hbGxpbnQ7Cgpwcm9jZWR1cmUgd3JpdGVBcnIoYXJyOnNpbXBsZSk7ClZhciBpOnNtYWxsaW50OwpiZWdpbgoJZm9yIGk6PTAgdG8gbi0xIGRvIHdyaXRlKGFycltpXSwgJyAnKTsKCXdyaXRlbG4oKTsKZW5kOwoKZnVuY3Rpb24gc3VtT2ZEaWdpdHNJbk51bShudW06c21hbGxpbnQpOnNtYWxsaW50OwpWYXIgaSwgdDpzbWFsbGludDsKYmVnaW4KCXQ6PTA7Cglmb3IgaTo9MCB0byA1I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