Let $\{A_i\}_{i\in I}$ be a collection of subsets of a topological space $X$, such that $X = \bigcup_{i \in I} A_i$. Let $f:X \rightarrow Y$ be a map of topological spaces such that the restriction of $f$ to each $A_i$ (equipped with the subspace topology induced from $X$) is continuous.
Show that if each $A_i$ is closed, and $I$ is finite, then $f$ is continuous.
I understand that this can be solved by the pasting lemma, but I don't see how I can do it using both hypotheses that $A_i$ is closed and $I$ finite.