fork download
  1. def digitSum(x):
  2. total = 0
  3. while x>0:
  4. total+=x%10
  5. x=x//10
  6. return total
  7.  
  8.  
  9. def solve(A):
  10. max_num = {}
  11. ans = -1
  12.  
  13. for x in A:
  14. s = digitSum(x)
  15.  
  16. if s in max_num:
  17. ans = max(ans, max_num[s] + x)
  18.  
  19. max_num[s] = max(max_num.get(s,0), x)
  20. return ans
  21.  
  22. def main():
  23. n = int(input())
  24. A = list(map(int, input().split()))
  25.  
  26. print(solve(A))
  27.  
  28. if __name__=="__main__":
  29. main()
Success #stdin #stdout 0.07s 14092KB
stdin
4
51 71 17 42
stdout
93