MianKing has nnn piles of stones and each pile has at most 333 stones, now he wants to merge all of these stones into one pile. In order to achieve his goal, each time MianKing can choose two piles of stones and merge them into a new pile, and the number of stones in the new pile is the sum of these two piles. Because it takes manpower to move stones, in each operation if the numbers of these two piles of stones are xxx and yyy respectively, MianKing should pay coins for it. Now MianKing wants to know the minimum amount of coins he need to pay to merge all of these stones into one pile.
MianKing has nnn piles of stones and each pile has at most 333 stones, now he wants to merge all of these stones into one pile. In order to achieve his goal, each time MianKing can choose two piles of stones and merge them into a new pile, and the number of stones in the new pile is the sum of these two piles. Because it takes manpower to move stones, in each operation if the numbers of these two piles of stones are xxx and yyy respectively, MianKing should pay (x mod 3)(y mod 3)(x~mod~3)(y~mod~3)(x mod 3)(y mod 3) coins for it. Now MianKing wants to know the minimum amount of coins he need to pay to merge all of these stones into one pile.