Statistical mechanics of Monte Carlo sampling and the sign problem