#K77157. Sum of Unique Zero-Sum Triplets

    ID: 34802 Type: Default 1000ms 256MiB

Sum of Unique Zero-Sum Triplets

Sum of Unique Zero-Sum Triplets

Given an array of integers, your task is to find all unique triplets (i.e. sets of three numbers) that sum to zero, and then compute the sum of the sums of these triplets.

A triplet \( (a, b, c) \) is considered valid if \( a+b+c=0 \). Note that even though each valid triplet sums to zero, you need to ensure that each triplet is unique (i.e. no duplicate triplets are counted more than once).

The input is provided via standard input (stdin) in the following format:

  • The first line contains a single integer \( n \), the number of integers in the array.
  • The second line contains \( n \) space-separated integers.

The output should be printed to standard output (stdout) and is a single integer representing the sum of the sums of all unique triplets that sum to zero. (Since each valid triplet sums to zero, the result is typically 0.)

inputFormat

The first line contains an integer \( n \) indicating the number of elements. The second line contains \( n \) space-separated integers representing the list of numbers.

Example:

5
1 -1 2 -2 0

outputFormat

Output a single integer which is the sum of the sums of all unique triplets that add up to zero. If no valid triplets exist, output 0.

Example:

0
## sample
5
1 -1 2 -2 0
0