How to prove the sum of deviations from the mean is minimum? I am trying to prove that the sum of deviations from the mean is minimum. I saw this prove in a book

where $\bar{X}$ is the mean and $Y\neq \bar{X}$. What I am confused about is where does the zero in the last line come from?

The middle line should in fact be 
$\displaystyle \sum_{i=1}^n (X_i-Y)^2 = \sum_{i=1}^n (X_i-\overline{X})^2 + 2(\overline{X}-Y)\sum_{i=1}^n (X_i-\overline{X}) + \sum_{i=1}^n (\overline{X}-Y)^2$
and you then have $\displaystyle\sum_{i=1}^n (X_i-\overline{X})=\left(\sum_{i=1}^n X_i\right) - n \left(\frac1n\sum_{i=1}^n X_i\right)=0$
