Let $S_n$ be the set of all permutations of $(1,2,\ldots,n)$. Then find :
\[ \max_{\sigma \in S_n} \left(\sum_{i=1}^{n} \sigma(i)\sigma(i+1)\right) \]
where $\sigma(n+1)=\sigma(1)$.
1. **Reformulate the Problem:**
   We need to find the maximum value of the sum:
   \[
   \max_{\sigma \in S_n} \left(\sum_{i=1}^{n} \sigma(i)\sigma(i+1)\right)
   \]
   where $\sigma(n+1) = \sigma(1)$. This can be reformulated by considering the sum of squares and differences:
   \[
   \sum_{i=1}^{n} \sigma(i)\sigma(i+1) = \frac{1}{2} \left( \sum_{i=1}^{n} \sigma(i)^2 + \sum_{i=1}^{n} \sigma(i+1)^2 - \sum_{i=1}^{n} (\sigma(i) - \sigma(i+1))^2 \right)
   \]
   Since $\sigma(n+1) = \sigma(1)$, the sums $\sum_{i=1}^{n} \sigma(i)^2$ and $\sum_{i=1}^{n} \sigma(i+1)^2$ are equal.

2. **Simplify the Expression:**
   \[
   \sum_{i=1}^{n} \sigma(i)\sigma(i+1) = \frac{1}{2} \left( 2 \sum_{i=1}^{n} \sigma(i)^2 - \sum_{i=1}^{n} (\sigma(i) - \sigma(i+1))^2 \right)
   \]
   \[
   = \sum_{i=1}^{n} \sigma(i)^2 - \frac{1}{2} \sum_{i=1}^{n} (\sigma(i) - \sigma(i+1))^2
   \]

3. **Calculate the Sum of Squares:**
   The sum of squares of the first $n$ natural numbers is:
   \[
   \sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}
   \]

4. **Minimize the Sum of Differences:**
   To maximize $\sum_{i=1}^{n} \sigma(i)\sigma(i+1)$, we need to minimize $\sum_{i=1}^{n} (\sigma(i) - \sigma(i+1))^2$. The minimum value of $\sum_{i=1}^{n} (\sigma(i) - \sigma(i+1))^2$ is achieved when the differences $\sigma(i) - \sigma(i+1)$ are as small as possible.

5. **Optimal Permutation:**
   For $n = 2k$, the optimal permutation is $1, 3, 5, \ldots, 2k-1, 2k, 2k-2, \ldots, 2$.
   For $n = 2k-1$, the optimal permutation is $1, 3, 5, \ldots, 2k-1, 2k-2, \ldots, 2$.

6. **Calculate the Maximum Sum:**
   Using the optimal permutation, the maximum sum is:
   \[
   \sum_{i=1}^{n} \sigma(i)\sigma(i+1) = \frac{1}{2} \left( 2 \sum_{i=1}^{n} \sigma(i)^2 - \sum_{i=1}^{n} (\sigma(i) - \sigma(i+1))^2 \right)
   \]
   \[
   = \frac{n(n+1)(2n+1)}{6} - (2n-3)
   \]

The final answer is \( \boxed{ \frac{n(n+1)(2n+1)}{6} - (2n-3) } \)