fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. ll sod(ll n77)
  5. {
  6. ll s77 = 0, r77;
  7. while(n77 != 0)
  8. {
  9. r77 = n77 % 10;
  10. s77 = s77 + r77;
  11. n77 = n77 / 10;
  12. }
  13. return s77;
  14. }
  15. bool cmpsod(int a77, int b77)
  16. {
  17.  
  18. if(sod(a77) < sod(b77))
  19. return true;
  20. else if(sod(a77) == sod(b77) && a77 > b77)
  21. return true;
  22. else
  23. return false;
  24.  
  25. }
  26. int main() {
  27. ll n77; cin >> n77;
  28. vector<ll> a77(n77);
  29. for(int i=0; i<n77; i++)
  30. {
  31. cin >> a77[i];
  32. }
  33. sort(a77.begin(), a77.end(), cmpsod);
  34. for(int i=0; i<n77; i++)
  35. {
  36. cout << a77[i] << " ";
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 5512KB
stdin
7
20 88 975 1 0 40 22
stdout
0 1 20 40 22 88 975