Solution (source code)

= Solution

Each on-shell particle satisfies $E_i^2-c^2|\mathbf p_i|^2=m_i^2c^4$. Thus for a selected subset $A$,
$$
\boxed{\sum_{i\in A}(E_i^2-c^2p_i^2)=c^4\sum_{i\in A}m_i^2.}
$$
It is Lorentz invariant when the same particle labels and physical states are compared, with each <energy> and momentum transformed as that particle's <four-momentum>. No requirement that the particles share a velocity is needed. Their energies can change under interactions while their individual fixed <rest masses> remain the same.

A frame-dependent rule for choosing “half” is more delicate: different subsets can have different sums of rest-mass squares. If every particle has the same <rest mass> and the number selected is fixed, however, the displayed sum is unchanged even if the selected identities change. In a massless plasma it is identically zero for every subset. The precise condition is constancy of the selected scalar sum, not a blanket claim that every frame-dependent selection must change it.