#P8772. Sum of Pairwise Products
Sum of Pairwise Products
Sum of Pairwise Products
Given n integers \(a_{1}, a_{2}, \dots, a_{n}\), your task is to compute the sum of the product of every pair, i.e.,
\[ S = a_{1} \cdot a_{2} + a_{1} \cdot a_{3} + \cdots + a_{1} \cdot a_{n} + a_{2} \cdot a_{3} + \cdots + a_{n-2} \cdot a_{n-1} + a_{n-2} \cdot a_{n} + a_{n-1} \cdot a_{n} \]
An efficient way to solve this problem is to use the formula:
\[ S = \frac{(a_{1} + a_{2} + \cdots + a_{n})^2 - (a_{1}^2 + a_{2}^2 + \cdots + a_{n}^2)}{2} \]
inputFormat
The first line of input contains an integer n, the number of integers.
The second line contains n integers separated by spaces.
outputFormat
Output a single integer: the sum of the product of every pair.
sample
3
1 2 3
11