# Doubts regarding work and pressure during adiabatic compression of a gas in a cylinder

So, I have doubts regarding the work done on the gas in this situation:

Let's say I have an ideal gas inside an adiabatic cylinder fitted with a piston of area $$A$$.
The piston has mass $$M$$.
Outside the cylinder there's atmospheric pressure $$p_{atm}$$.
Suppose the gas and the piston are at equilibrium and the piston is at a distance $$h$$ from the bottom of the cylinder. Then, a mass $$m$$ is put on the piston, which compresses the gas until equilibrium is reached again.

In calculating the work done on the gas by the enviroment, which is: $$W =\int p \,dV$$ I would assume that I should use the external pressure that can be written as: $$p_{ext} = p_{atm} + \frac{mg}{A} + \frac{Mg}{A}$$
Therefore, the work becomes: $$W = \big(p_{atm} + \frac{mg}{A} + \frac{Mg}{A}\big) \Delta V$$ Is this right? The fact that there's a contribution to the work given by atmospheric pressure and the weight of the piston bugs me a little, because before the mass $$m$$ was put on the cylinder, everything was at rest. To me, it doesn't feel right that something other than the mass $$m$$ is doing work on the cylinder.
Should I replace the pressure in the work integral with just the pressure given by the mass $$m$$, instead? Because that's the difference in pressure that breaks the equilibrium? In that case the work would become: $$W_? = \frac{mg}{A} \Delta V$$ Now, if I call the linear displacement of the piston $$x$$. The final volume of the gas would be $$V_1 = (h - x) A$$.
Since the initial volume is $$V_0 = h A$$, $$\Delta V$$ becomes:$$\Delta V = V_1 - V_0 = - A x$$ Therefore the work, following this reasoning, would be:$$W_? =\frac{mg}{A} (-Ax) = -mgx$$ To me, this makes more sense because it means that the work done on the gas is equal to the potential energy that the mass $$m$$ loses going from $$h$$ to $$h-x$$.
Could you explain to me which is correct, $$W$$ or $$W_?$$ and why?
Thanks for taking the time