BAMO 2019 Problem 4

Let S be a finite set of nonzero real numbers, and let f: S \rightarrow S be a function with the following property: for each x \in S, either

f(f(x))=x+f(x) \quad \text { or } \quad f(f(x))=\frac{x+f(x)}{2}

Prove that f(x)=x for all x \in S.