Moving average algorithm that conserves integral
Why do you want to preserve the the sum? Is it to make the filter with the same gain everywhere?

If you want to keep the sum the same you need to pad the beginning and the end of the signal as: 
$$
.... ,y_2,y_1,y_0 ---- y_0,y_1, y_2,...., y_n ---- y_n, y_{n-1},y_{n-2}...
$$
Example:
suppose that you have 6 samples

$$
y_0, y_1, y_2, y_3,y_4, y_5
$$

and you want to to do a moving average with length 5:

Special treatment at the beginning:

$$
\hat y_0   = (y_1  +  y_0 +  y_0 +  y_1 + y_2)/5,
$$
$$
\hat y_1   = (y_0  +  y_0 +  y_1 +  y_2 + y_3)/5
$$

Normal filtering:

$$
\hat y_2   = (y_0  +  y_1 +   y_2 + y_3  + y_4)/5,
$$
$$
\hat y_3   = (y_1  +  y_2 +   y_3 + y_4  + y_5)/5,
$$

Special treatment at the end:

$$
\hat y_4   = (y_2  +  y_3 +   y_4 + y_5  + y_5)/5,
$$
$$
\hat y_5   = (y_3  +  y_4 +   y_5 +  y_5 + y_4)/5,
$$