This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.
As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.
There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.
Constraints
All values in
input are integers.
2 \leq N \leq 50
0 \leq d_i \leq 100
Input
Input is given from Standard Input in the following format:
N
d_1 d_2 ... d_N
Output
Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.
Examples
Input
3
3 1 2
Output
11
Input
7
5 0 7 8 3 3 2
Output
312
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to compute sum over all pairs (i < j) of d_i * d_j. This is a classic problem: the sum of products of all pairs equals ( (sum)^2 - sum of squares ) / 2. But we can also double loop since N <= 50. The constraints are tiny, so we can just compute straightforward.
But careful: The problem states: "for each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these values." That is exactly sum over i<j d_i * d_j.
Example2: compute? Let's check: sum = 5+0+7+8+3+3+2 = 28. squares sum = 25+0+49+64+9+9+4 = 160. (sum^2 - sq_sum)/2 = (784 - 160)/2 = 624/2 = 312. So output 312.
So we can implement.
Edge cases: d_i = 0 possible, N=2, sum will be d1*d2.
Implementation: read N, read list of ints. Use two nested loops for i in range(N), for j in range(i+1,N), accumulate product. Or use formula.
We'll implement with double loop because it's simple.
Time complexity O(N^2) trivial.
Now, output as integer.
We'll produce Python code.
Testing with given examples.
We'll also consider potential overflow: N max 50, d_i max 100, max pair product = 100*100=10000, number of pairs ~1225, sum up to ~12 million fits in int.
So all good.
Thus solution: read input, compute ans = sum(d[i]*d[j] for i in range(N) for j in range(i+1, N)), print(ans).
Now, generate final answer.
But the instruction: "Write a complete solution." So we produce final code.
We need to include explanation? Typically we just output code. However the user wants "Solve the following programming problem." So we can provide solution in code. The answer should be just code? In many contexts, we provide explanation and then code. But the user said: "You are an AI. Solve the following programming problem. Provide a complete solution in the form of code that can be run as is. Include necessary imports and define main function if applicable." So we should provide complete code with main function, reading from stdin, writing to stdout.
Thus: We'll write in Python:
def main():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
N = int(data[0])
d = list(map(int, data[1:1+N]))
ans = 0
for i in range(N):
for j in range(i+1, N):
ans += d[i] * d[j]
print(ans)
if name == "main":
main()
That's fine.
Alternatively we could compute via formula. But double loop is fine.
We'll also note that the input may have more numbers? Actually there are exactly N numbers after N, but reading first N numbers after N is safe.
Thus final answer. ```python
import sys
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
N = int(data[0])
d = list(map(int, data[1:1 + N]))
code
ans = 0
for i in range(N):
for j in range(i + 1, N):
ans += d[i] * d[j]
print(ans)