Since this is an expansion and the external pressure is not zero, work is being done by the system, therefore energy is leaving the system and thus the sign of the work is negative. Here we can just use the general definition of work and realize that Pin and Pex are the same and that for an ideal gas, we can use the ideal gas law: