Given that Sasha and Anna can only perform reversal and concatenation processes, the numerical value of each digit of each element will not change. Since all non-zero digits would become concatenated eventually, only the zero digits would matter to our calculation.
For Anna’s operation, the choosen element will be removed of it suffix of zeros (since prefix zeros will be eliminated after reversal). Therefore, Anna’s optimal strategy would be to reverse the element with most suffix zeros among the remaining elements.
For Sasha, the concatenation process actually shields the zero suffix of one element from Anna’s reversals. That is, when integers x , y x,y x,y are concatenated, the suffix zeros of x x x can no longer be eliminated through reverse operations. Therefore, Sasha’s optimal strategy would be to concatenate the remaining element with the longest zero suffix with any other element.
In light of the girls’ optimal strategies, it is obvious that Anna would eliminate the zeros trailing behind the 1st, 3rd, 5th… elements (after we sorted the elements in decreasing order according to the length of their zero suffix). Subsequently, we can easily calculate how many digits there are in total and how many zeros Anna has eliminated.
#include<cstdio>