# Changing the order of summation in a triple sum

I am trying to simplify an expression that came up when I was trying to calculate the energy of a multiple pendulum system. The expression I have is $$\sum_{k=1}^N \sum_{i=1}^k \sum_{j=1}^k m_k v_i v_j \cos(\theta_i-\theta_j)$$ but I want to change the order of summation so that the sum over $$k$$ is now on the inside. After a lot of trial and error, I seem to have found that $$\sum_{k=1}^N \sum_{i=1}^k \sum_{j=1}^k m_k v_i v_j \cos(\theta_i-\theta_j)=\sum_{i=1}^N \sum_{j=1}^N \sum_{k=\max(i,j)}^N m_k v_i v_j \cos(\theta_i-\theta_j)$$ works, but I have no idea where this result comes from or how one would prove it. Any help would be appreciated, thanks!

Let us examine the set over which the summation is taken. The values of $$i$$ and $$j$$ are independent of each other, but both depends on $$k$$. \begin{align} 1\leq& k\leq N\\ 1\leq &i\leq k\\ 1\leq &j\leq k \end{align} Next, we change the bounds on $$k,i,j$$ such that $$k$$ is bounded in terms of $$i$$ and $$j$$, and bounds on $$i$$ and $$j$$ are independent of $$k$$. It is easy to see that $$1\leq i,j\leq N$$. Further, we have \begin{align} 1\leq &i\leq k \text{ and } 1\leq k\leq N \implies i\leq k\leq N\\ 1\leq &j\leq k \text{ and } 1\leq k\leq N \implies j\leq k\leq N. \end{align} Thus, we get $$$$\max\{i,j\}\leq k\leq N.$$$$ Therefore, the result. If $$i$$ and $$j$$ were dependent, we have to do this cyclically, i.e., first find bound on $$i$$ which is independent of $$j$$ and $$k$$. Then, bound $$j$$ interms of $$i$$, and finally, bound $$k$$ using $$i$$ and $$j$$.
\begin{align*} \sum_{k=1}^N \sum_{i=1}^k \sum_{j=1}^k m_k v_i v_j \cos(\theta_i-\theta_j) &=\sum_{\color{blue}{1\leq i,j\leq k\leq N} }m_k v_i v_j \cos(\theta_i-\theta_j)\\ &=\sum_{i=1}^N \sum_{j=1}^N\sum_{k=\max\{i,j\}}^N m_k v_i v_j \cos(\theta_i-\theta_j) \end{align*}