No $f\in C$ exists with $f(0)=3$. The maximum is thus (since $f(0)\in\mathbb Z$) exactly $2$, reached for example at $f(x)=1+\cos(2\pi x)$.
The main facts useful in proving this will be these:
- For any $f\in C$ and any $x\in\mathbb R$, $0\leq f(x)\leq 3$.
- For any $f\in C$, $\sum a_n^2<4$.
- If $f(0)=3$, then $\sum_{n\equiv 1\pmod 3}na_n=\sum_{n\equiv 2\pmod 3}na_n$.
To prove (1), we first see that, for any $n$ with $3\nmid n$ and any $x\in\mathbb R$,
$$\cos(2\pi nx)+\cos\left(2\pi n\left(x+\frac 13\right)\right)+\cos\left(2\pi n\left(x+\frac 23\right)\right)=0$$
(the easiest way to see this might be by passing to complex numbers and using that $1+\omega+\omega^2=0$ when $\omega=e^{2\pi i/3}$). This means that, for any $x\in\mathbb R$,
$$f(x)+f\left(x+\frac 13\right)+f\left(x+\frac 23\right)=3;\tag{$\star$}$$
since each term on the left side is nonnegative, (1) follows.
To prove (2), let $X$ be a random variable with distribution $\operatorname{Unif}([0,1))$. One has $\mathbb E[f(X)]=1$ and
\begin{align*}
\mathbb E[(f(X)-1)^2]&=\sum_{m,n}a_ma_n\mathbb E[\cos(2\pi mX)\cos(2\pi nX)]\\
&=\frac 12\sum_{n\geq 1}a_n^2.
\end{align*}
Now, since $0\leq f(x)\leq 3$ for all $x$,
\begin{align*}
\frac 94=\left(\frac 32\right)^2
&>\mathbb E\left[\left(f(X)-\frac 32\right)^2\right]\\
&=\mathbb E[(f(X)-1)^2]-\mathbb E[f(X)]+\frac 54\\
&=\frac 14+\frac12\sum_{n\geq 1}a_n^2
\end{align*}
(the first inequality is because $|f(X)-3/2|$ is upper-bounded by $3/2$, and cannot always be exactly $3/2$ as $f$ is continuous). This implies that
$$\sum_{n\geq 1}a_n^2<4.$$
To prove (3), we see that ($\star$) implies that $f(1/3)=0$. Since $f(x)\geq 0$ in a neighborhood of $1/3$, we must have $f'(1/3)=0$. Since
$$\frac{d}{dx}\cos(2\pi nx)\bigg|_{x=1/3}=-2\pi n\sin\left(\frac{2\pi n}3\right)=\begin{cases}0&\text{if }n\equiv 0\pmod 3\\-\pi n\sqrt3&\text{if }n\equiv 1\pmod 3\\\pi n\sqrt3&\text{if }n\equiv 2\pmod 3,\end{cases}$$
(3) follows from summing and dividing by $\pi\sqrt 3$.
Now, if $f\in C$ with $f(0)=3$, we have $\sum a_n=2$. Since $\sum a_n\equiv \sum a_n^2\pmod 2$ and $\sum a_n^2>0$, this implies (with (2)) that $\sum a_n^2=2$. Thus all but two of the $a_n$ are zero and the other two are one, i.e.
$$f(x)=1+\cos(2\pi mx)+\cos(2\pi nx)$$
for some distinct $m,n\in\mathbb N$. However, (3) then implies either $m+n=0$ or $m=n$, a contradiction. So, there is no $f\in C$ with $f(0)=3$.