First, let us prove the following lemma.
Lemma: For any set of positive integers m1,m2,…,mk, we have the inequality
S(i=0∑kmi)≤i=0∑kS(mi).
Furthermore, equality holds in the inequality above if the following condition is satisfied:
For any nonnegative integer j, the 10j-th digit of mi=0 for all but at most one i.
Proof: Whenever there is a carry-over of digits when the addition ∑i=0kmi is performed, there occurs the reduction by 9 for the value of the sum of digits. Thus, the inequality is satisfied. If the additional condition is satisfied then no carry-over of digits will occur and we get the equality.
For a quintuple (a1,…,a5) of non-negative integers satisfying the condition a1+⋯+a5=5 (in the sequel, we simply call such "a quintuple"), denote by f(a1,…,a5) the coefficient of the term X1a1⋯X5a5 in the expansion of the multinomial (X1+X2+X3+X4+X5)5. The values of f(a1,…,a5) are given by a1!a2!⋯a5!5! for each quintuple (a1,…,a5) (we use the convention 0!=1).
Now, since n satisfies the condition S(n)=5, we can represent n in the form n=10k1+10k2+10k3+10k4+10k5, using non-negative integers k1,k2,k3,k4,k5. (Note that the kj's need not have distinct values.) Then, using the multinomial expansion we can write
n5=∑f(a1,…,a5)10a1k1+⋯+a5k5,
where the sum is taken over all possible quintuples (a1,…,a5). By using the Lemma, we then get
S(n5)≤∑S(f(a1,…,a5)⋅10a1k1+⋯+a5k5)=∑S(f(a1,…,a5))(1)
Here again the sums are taken over all possible quintuples (a1,…,a5).
For a quintuple (a1,…,a5), let us denote by (b1,…,b5) a quintuple obtained by rearranging the entries in decreasing order. Then, there are only the following 7 possibilities for (b1,…,b5):
(5,0,0,0,0), (4,1,0,0,0), (3,2,0,0,0), (3,1,1,0,0),(2,2,1,0,0), (2,1,1,1,0), (1,1,1,1,1),
and each of these (b1,…,b5) corresponds to 5, 20, 20, 30, 30, 20, 1 different (a1,…,a5)'s, respectively. Furthermore, we have f(a1,…,a5)=f(b1,…,b5) if (b1,…,b5) is the decreasing arrangement of (a1,…,a5). The value of f(b1,…,b5) for the 7 different (b1,…,b5)'s are given by 1, 5, 10, 20, 30, 60, 120, respectively. Therefore, we have that the value of ∑S(f(a1,…,a5)) is given by
5⋅S(1)+20⋅S(5)+20⋅S(10)+30⋅S(20)+30⋅S(30)+20⋅S(60)+1⋅S(120),which equals 5⋅1+20⋅5+20⋅1+30⋅2+30⋅3+20⋅6+1⋅3=398. Therefore,we have S(n5)≤398.
Finally, let us show that it is possible to construct n for which S(n5)=398. Choose k1,…,k5 suitably so that for any pair of different quintuples (a1,…,a5) the corresponding values of a1k1+⋯+a5k5 differ at least by 3 (for instance, let ki=3×6i−1 for i=1,…,5). Then, since f(a1,…,a5)<103 is satisfied for any quintuple (a1,…,a5), for any non-negative j the coefficient of 10j term in the sum ∑f(a1,…,a5)10a1k1+⋯+a5k5 is 0 except for at most one quintuple (a1,…,a5).
Therefore, by the Lemma, we have the equality in the inequality (1). This shows that 398 is the desired answer to the problem.