Code Jam 2008 1C: Text Messaging Outrage
Intuition: if f(a) >= f(b), then k(a) <= k(b) so that total # is minimized
Proof: Suppose there exists f(a) >= f(b), and k(a) > k(b), swapping the key of a and b will reduce the total #. Thus, we can repeat the process and keep reducing the total #,i.e., an arrangement satifies the condition is better than all arrangments not satisfying the condition, this means the condition is optimal
Note that sorting is increasing order by default. In this case we need decreasing order, or we can start checking from tail to head instead of head to tail