In this paper we solve the following problem: Given a positive integer f and L weights (real numbers), nd a partition with f classes of the multiset of weights such that the sum of the costs of the optimum m-ary Hu man trees built for every class of is minimum. An application to the optimal extendibility problem for pre x codes is proposed.