• + 0 comments

    My Python solution:

    if __name__ == '__main__':
        n = int(input().strip())
        
        nums = [i for i in range(1, n+1) if n%i==0]
        best = 0
        max_sum = 0
        for x in nums:
            soma = sum(map(int,str(x)))
            if soma > max_sum:
                best, max_sum = x, soma
            elif soma == max_sum:
                best = min(x, best)
        
        print(best)