12. Given a positive integer $n \geqslant 3$. Let $A_{1}, A_{2}, \cdots, A_{n}$ (allowing overlaps) be points on the unit circle $\Omega$ with center $O$, and let $d_{k}=\overrightarrow{O A_{k}} \cdot \overrightarrow{A_{k-1} A_{k+1}}(k=1,2, \cdots, n)$, where $A_{0}=A_{n}, A_{n+1}=A_{1}$. Find the maximum value of $S=\left|d_{1}\right|+\left|d_{2}\right|+\cdots+\left|d_{n}\right|$.
Consider the following three cases.
(1) When $4 \mid n$, by $\left|d_{k}\right| \leqslant\left|\overrightarrow{O A_{k}}\right|\left|\overrightarrow{A_{k-1} A_{k+1}}\right|$
$$
=\left|\overrightarrow{A_{k-1} A_{k+1}}\right| \leqslant 2 \quad (k=1,2, \cdots, n) \Rightarrow S \leqslant 2 n.
$$

As shown in Figure (a), take a diameter $P Q$ of the unit circle $\Omega$, and let $A_{1}, A_{3}, A_{5}, \cdots, A_{n-1}$ alternately take points $P, Q$, and $A_{2}, A_{4}, A_{6}, \cdots, A_{n}$ also alternately take points $P, Q$. At this time, each $d_{k}$ is $\pm 2$, and $S$ reaches the maximum value $2 n$.
(2) When $n$ is odd, let $a_{k}=\overrightarrow{O A_{k-1}} \cdot \overrightarrow{O A_{k}}(k=1,2, \cdots, n)$, then $d_{k}=\overrightarrow{O A_{k}} \cdot\left(\overrightarrow{O A_{k+1}}-\overrightarrow{O A_{k-1}}\right)=a_{k+1}-a_{k}(k=1,2, \cdots, n)$. Since $n$ is odd, there must exist $j \in\{1,2, \cdots, n\}$, such that $a_{j}-a_{j-1}$ and $a_{j+1}-a_{j}$ are not of opposite signs. Without loss of generality, assume $a_{1}-a_{n}$ and $a_{2}-a_{1}$ are not of opposite signs, then $S=\left|a_{1}-a_{n}\right|+\left|a_{2}-a_{1}\right|+\sum_{k=2}^{n-1}\left|d_{k}\right|$ $=\left|a_{2}-a_{n}\right|+\sum_{k=2}^{n-1}\left|d_{k}\right| \leqslant\left|a_{2}\right|+\left|a_{n}\right|+\sum_{k=2}^{n-1} 2=1+1+2(n-2)=2 n-2$.

Figures (b) and (c) show the cases when $n \equiv 1(\bmod 4)$ and $n \equiv 3(\bmod 4)$, respectively. In both cases, $\left|d_{1}\right|+\left|d_{n}\right|=\left|\overrightarrow{O A_{1}} \cdot \overrightarrow{A_{n} A_{2}}\right|+\left|\overrightarrow{O A_{n}} \cdot \overrightarrow{A_{n-1} A_{1}}\right|=2$, and $S$ reaches the maximum value $2 n-2$.
(3) When $n \equiv 2(\bmod 4)$, let $n=4 m+2$. For $k=1,2, \cdots, n$, let $\theta_{k}\left(0 \leqslant \theta_{k}<2 \pi\right)$ be the smallest non-negative angle swept by rotating from $\overrightarrow{O A_{k-1}}$ to $\overrightarrow{O A_{k+1}}$ in the counterclockwise direction. It is easy to see that there exist $l_{1}, l_{2} \in\{0,1, \cdots, m\}$, such that $\left\{\begin{array}{l}\theta_{1}+\theta_{3}+\cdots+\theta_{4 m+1}=2 l_{1} \pi, \\ \theta_{2}+\theta_{4}+\cdots+\theta_{4 m+2}=2 l_{2} \pi .\end{array}\right.$
Combining the convexity of the function $y=\sin x$ in the interval $[0, \pi)$, we get
$$
\begin{array}{l}
S=\sum_{k=1}^{n}\left|\overrightarrow{O A_{k}} \cdot \overrightarrow{A_{k-1} A_{k+1}}\right| \leqslant \sum_{k=1}^{n}\left|\overrightarrow{A_{k-1} A_{k+1}}\right| \\
=\sum_{j=1}^{2 m+1}\left|\overrightarrow{A_{2 j-2} A_{2 j}}\right|+\sum_{j=1}^{2 m+1}\left|\overrightarrow{A_{2 j-1} A_{2 j+1}}\right|=\sum_{j=1}^{2 m+1} 2 \sin \frac{\theta_{2 j-1}}{2}+\sum_{j=1}^{2 m+1} 2 \sin \frac{\theta_{2 j}}{2} \\
\leqslant 2(2 m+1) \sin \frac{\theta_{1}+\theta_{3}+\cdots+\theta_{4 m+1}}{2(2 m+1)}+2(2 m+1) \sin \frac{\theta_{2}+\theta_{4}+\cdots+\theta_{4 m+2}}{2(2 m+1)} \\
=2(2 m+1)\left(\sin \frac{l_{1} \pi}{2 m+1}+\sin \frac{l_{2} \pi}{2 m+1}\right) \leqslant 4(2 m+1) \sin \frac{m \pi}{2 m+1}=2 n \sin \frac{(n-2) \pi}{2 n} .
\end{array}
$$

Equality holds when $\overrightarrow{O A_{k}} / / \overrightarrow{A_{k-1} A_{k+1}}$ and $\theta_{1}=\theta_{2}=\cdots=\theta_{n}=\frac{2 m \pi}{2 m+1}=\frac{(n-2) \pi}{n}$. As shown in the figure, take points $A_{1}, A_{2}$ on the unit circle $\omega$ such that $\overrightarrow{O A_{1}}$ rotates $\frac{\pi}{n}$ clockwise to coincide with $\overrightarrow{O A_{2}}$, and let $\theta_{1}, \theta_{2}, \cdots, \theta_{n}$ all be $\frac{(n-2) \pi}{n}$. At this time, the three inequalities in the above formula all hold with equality, thus $S$ reaches the maximum value $2 n \sin \frac{(n-2) \pi}{2 n}$. Combining (1), (2), and (3), we get
$$
S_{\max }=\left\{\begin{array}{ll}
2 n, & n \equiv 0(\bmod 4), \\
2 n-2, & n \equiv 1,3(\bmod 4), \\
2 n \sin \frac{(n-2) \pi}{2 n}, & n \equiv 2(\bmod 4) .
\end{array}\right.
$$