ACM ICPC World Finals 2015
Shortest judge solution: 1206 bytes. Shortest team solution (during contest): 1054 bytes.
This problem asks for the cost of a Huffman coding of the 4n strings of length n over the alphabet {S, C, R, F}, where the probability of a string with fS S’s, fC C’s, fR R’s, and fF F’s is psunnyfS · pcloudyfC · prainyfR · pfrogsfF. Unfortunately, 4n is so large that we can not simulate the Huffman coding procedure naively. However, due to the product structure of the distribution, there will be many nodes in the Huffman coding having the same probability, so we can group these together and just keep their count.
Initially, there are n(fS, fC, fR, fF) = n!/(fS! fC! fR! fF!) nodes with the probability psunnyfS · pcloudyfC · prainyfR · pfrogsfF (for each combination of non-negative values fS, fC, fR, fF summing up to n). Now we proceed with Huffman encoding: pick the smallest probability node, say this probability is p. If there are K nodes with this probability, they give rise to ⌊K/2⌋ nodes of probability 2p, and if K is odd then one remaining node of probability p gets matched with one of the nodes with the second smallest probability value.