If $a_1, a_2, \ldots$ is a sequence of real numbers such that for all $n$,  $$\sum_{k = 1}^n a_k \left( \frac{k}{n} \right)^2 = 1,$$  find the smallest $n$ such that $a_n < \frac{1}{2018}$.

Let's think step by step and output the final answer within \boxed{}.
user