https://www.acmicpc.net/problem/1431
import java.io.*;
import java.util.*;
/**
* Solution
*/
class Node implements Comparable<Node>{
String s;
int cnt;
Node(String s, int cnt)
{
this.s = s;
this.cnt = cnt;
}
@Override
public int compareTo(Node node)
{
if (this.s.length() == node.s.length()) {
if (this.cnt == node.cnt) {
return this.s.compareTo(node.s);
}
return this.cnt - node.cnt;
}
return this.s.length() - node.s.length();
}
@Override
public String toString()
{
return s;
}
}
public class Main {
static int N;
static PriorityQueue<Node>pq = new PriorityQueue<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
String number = br.readLine();
String s = number.replaceAll("[^0-9]","");
int sum = 0;
for (int j = 0; j < s.length(); j++) {
sum += s.charAt(j) -'0';
}
pq.offer(new Node(number,sum));
}
while (!pq.isEmpty()) {
String res = pq.poll().s;
System.out.println(res);
}
}
}