Answer: M=3n−4 and f(a1,a2,…,an)=3n−4 if and only if a1=a2=…=an=3.
First, we note that for every distribution, there exists a move such that max(a1,…,an) increases by at least one, unless all stones are in one box. Indeed, choose a box where the maximum is achieved, choose another non-empty box, and distribute the stones from that box so that at least one stone goes to the first box. This implies that f(a1,a2,…,an)≤3n−max(a1,…,an). In particular, if max(a1,…,an)≥5, then f(a1,a2,…,an)≤3n−5. The rest of the proof follows from four claims.
Claim 1. If max(a1,…,an)=4, then f(a1,a2,…,an)≤3n−5.
Proof. Let A1 be the box with the most stones, and A2 the box with the second-highest number of stones. This means that a1=4 and a2≥n−13n−4=3−n−11. Since a2 is an integer and n≥3, this means that a2≥3. We distribute every other box with at least 2 stones so that A1 and A2 each receive one stone. We repeat this until there are no more boxes with at least 2 stones, except for A1 and A2. For this distribution b1,…,bn, we have b3+⋯+bn≤n−2. This means that b1+b2≥3n−(n−2)=2n+2 and since b1−b2=a1−a2≤1, we find that b2=21(b2+b2)≥21(b1+b2−1)≥21(2n+1)=n+21. Since b2 is an integer, we find that b2≥n+1. Now we can make a move where we distribute the stones from A2, giving A1 2 stones. Then we finish with moves where A1 receives at least one stone each time. Since A1 receives at least one stone per move and a move involves at least two stones, the number of moves made is at most 3n−5.
Claim 2. If max(a1,…,an)=3, then f(a1,a2,…,an)≤3n−4.
Proof. We make an arbitrary move and apply Claim 1 to the result. Then we are done in at most 1+(3n−5)=3n−4 moves. Alternatively, we can give a concrete series of 3n−4 moves. If max(a1,…,an)=3, each box has exactly 3 stones. We start with n−2 moves where we choose Ai with i≥3 and distribute the three stones over A1,A2, and Ai. Then the first two boxes each have n+1 stones, so we can make a move where we distribute the n+1 stones from the second box so that the first box gets 2 stones. Since A1 receives at least one stone per move and a move involves at least two stones, the number of moves made is at most 3n−4.
Claim 3. There are no moves such that the maximum max(a1,…,an) increases by 3 or more.
Proof. To make a move where a box receives more than 3 stones, the chosen box would need to contain at least 3n+1 stones. This contradicts the fact that there are only 3n stones. To make a move where a box receives 3 stones, the chosen box must contain at least 2n+1 stones. A box that achieves the new maximum after this move must also contain at least 2n+1 stones. This is again a contradiction.
Claim 4. If max(a1,…,an)=3, then f(a1,a2,…,an)≥3n−4.
Proof. Suppose we can get all the stones into one box in 3n−5 or fewer turns. By Claim 3, there are no moves where the maximum increases by 3 or more. This means that there are at least two moves where the maximum increases by 2. We call such moves large moves. Let the first large move be in turn i and the last large move be in turn j. Since a large move can only be performed with a box containing at least n+1 stones and each box starts with 3 stones, we have i−1≥(n+1)−3 or i≥n−1.
Let m be the number of empty boxes at the start of move j. Since each box has at least 1 stone after move i, we must have made at least one move per empty box after that. This means that (j−1)−i≥m. Since each box receives at least 1 stone, there are at most m boxes with 1 stone after move j; the rest contain at least 2. Since we do not make any large moves after move j, it takes at least m+2(n−1−m)=2(n−1)−m moves to empty n−1 boxes. The total number of moves is thus at least
i+(j−i)+2(n−1)−m≥(n−1)+(m+1)+2(n−1)−m=3n−2,
which contradicts our assumption that we could do it in 3n−5 moves.