1 | #include <bits/stdc++.h> |
2 | |
3 | using namespace std; |
4 | |
5 | int digitSum(int x) { |
6 | if (x < 10) { |
7 | return x; |
8 | } else { |
9 | return digitSum(x / 10) + x % 10; |
10 | } |
11 | } |
12 | |
13 | bool cmp(int x, int y) { |
14 | return (digitSum(x) < digitSum(y) || digitSum(x) == digitSum(y) && x < y); |
15 | } |
16 | |
17 | int main() { |
18 | std::ios_base::sync_with_stdio(false); |
19 | cin.tie(0); |
20 | cout.tie(0); |
21 | |
22 | int n; |
23 | cin >> n; |
24 | int a[n]; |
25 | for (int i = 0; i < n; ++i) { |
26 | cin >> a[i]; |
27 | } |
28 | |
29 | |
30 | sort(a + 0, a + n, &cmp); |
31 | |
32 | for (int i = 0; i < n; ++i) { |
33 | cout << a[i] << " "; |
34 | } |
35 | |
36 | return 0; |
37 | } |
38 | |
39 | |