Just like Wojciech said, there is a bijection between calculating Huffman Code and your problem. As you can see, we can treat your problem as:
Find binary rooted tree with $n$ weighted (weights $a_1,...,a_n$) leafs, such that sum of
distances from leafs to root multiplied by their weight would be
This problem can be solved by calculating Huffman Code. Now let's see why we can use that algorithm to solve our problem. We will treat each deck as a leaf and each shuffling as an inner verticle. If all inner vertices have exactly 2 kids then such tree is a result of some shuffling procedures. We can easily prove that every Huffman Code Tree don't have inner verticles with only one child (because we could reduce that tree otherwise).
For further reading:
In this lecture note you can read prove that you can use your algorithm to build Huffman Code.