An IMO training problem: Living in an overpopulated apartment Here is a problem from the book 102 Combinatorial Problems: From the Training of the USA IMO Team by Titu Andreescu and Zuming Feng:

Original problem: A total of $119$ residents live in a building with $120$ apartments. We call an apartment overpopulated if there are at least $15$ people living there. Every day the inhabitants of an overpopulated apartment have a quarrel and each goes off to a different apartment in the building (so they can avoid each other). Is it true that this process will necessarily be completed someday?
Extension (Self-made): If after $n$ days the process is completed, what is the maximum value of $n$?

Solution from the book
Let $p_1,p_2,\cdots,p_{120}$ denote the $120$ apartments, and let $a_i$ denote
the number of residents in apartment $p_i$. We consider the quantity
$$\color{red}{\textrm{S=$\frac{a_1(a_1-1)}{2}+\frac{a_2(a_2-1)}{2}+\cdots+\frac{a_{120}(a_{120}-1)}{2}$}}$$
(Assume that all the residents in an apartment shake hand with each other at the beginning of the day, then quantity $S$ denotes the number of the handshakes in that day.) If all $a_i < 15$, then the process is completed and we are done. If not, without loss of generality, we assume that $a_1\ge15$ and that
the inhabitants in $p_i$ go off to different apartments in the building. Assume that they go to apartments $p_{i_{1}},p_{i_2},\cdots,p_{i_{a_1}}$. $\color{blue}{\textrm{On the next day, the quantity is changed by an amount of  }}$ $$\color{blue}{\textrm{$a_{i_1}+a_{i_2}+\cdots+a_{i_{a_1}}-\frac{a_1(a_1-1)}{2}$}}$$ which is positive as $$a_{i_1}+a_{i_2}+\cdots+a_{i_{a_1}}\le119-a_1\le119-15=104$$
and $$\frac{a_1(a_1-1)}{2}\ge\frac{15\cdot14}{2}=105$$
Hence the quantity is decreasing during this process. On the other hand, $S$
starts as a certain finite number and $S$ is nonnegative. Therefore this process
has to be completed someday. $\blacksquare$
My question
I don't understand the solution (especially the texts which are in colored), even after trying to understand it for almost two days. I have the following questions:

*

*Why is $S$ needed to be considered?

*Shouldn't the quantity be same all the time, as there is no change in the number of residents? (text in $\color{blue}{\textrm{blue}}$)

*And how to solve the extension of the problem?

Can someone explain the solution or provide a different and easier solution?
Thanks in advance.

It may be easier to follow the solution in the context of a graph.
In the solution, $S$ is the number of edges of the graph $G$ where the graph’s vertices represent the residents and the edges mean “live in the same apartment.” The graph $G$ at any moment is a union of disjoint complete graphs, one for each apartment. A complete graph on $a$ vertices has $a(a-1)\over2$ edges.
The blue quantity is the change in the number of edges of $G$ if all residents of one apartment leave that apartment and move to separate other apartments.
The specific calculations show that when an “overpopulated” apartment is vacated, the number of edges of $G$ decreases.
