Problem:
Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5

Solution in Go (1.6) (judge verdict: Time Limit Exceeded)

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var isProduct = false

var memo = map[int]int{}

func main() {
	nextReader = newScanner()

	N := nextInt()
	A := nextInts(N)

	d := make(map[int]int, N)

	for _, a := range A {
		d[a]++
	}

	for i := 0; i < N; i++ {
		memo[i] = (i * (i - 1.0) / 2.0)
	}

	for i := 0; i < N; i++ {
		t := 0

		for key, s := range d {
			if key == A[i] {
				t += memo[s-1]
			} else {
				t += memo[s]
			}
		}

		fmt.Println(t)
	}

}

/* --------------------------------------------------------------------------
 *  sort
 * ------------------------------------------------------------------------- */

// type S struct {
//   sort int
// }
// type Sorter []S

// func (a Sorter) Len() int           { return len(a) }
// func (a Sorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
// func (a Sorter) Less(i, j int) bool { return a[i].sort < a[j].sort }

// type Sorter []Sortable

// func (a Sorter) Len() int           { return len(a) }
// func (a Sorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
// func (a Sorter) Less(i, j int) bool { return a[i].Axis < a[j].Axis }

/* --------------------------------------------------------------------------
 *  math
 * ------------------------------------------------------------------------- */
func lcm(x, y int) int {
	return x * y / gcd(x, y)
}

func gcd(x, y int) int {
	tmp := x % y
	if tmp > 0 {
		return gcd(y, tmp)
	} else {
		return y
	}
}

func abs(i int) int {
	return int(math.Abs(float64(i)))
}

//prime
type PrimeGenerator struct {
	Next func() uint32
}

func NewPrimeGenerator() *PrimeGenerator {
	primeGenerator := PrimeGenerator{}

	multiples := map[uint32]uint32{}
	var num uint32
	var d uint32 // ←ココ
	primeGenerator.Next = func() uint32 {
		if num == 0 {
			num = 1
			return 2
		}
		if d == 0 { // ←ココ
			d = 4
			return 3
		}

		for {
			num += d
			d = 6 - d
			var k uint32 = 2 // ←ココ

			factor, hasFactor := multiples[num]
			if hasFactor {
				delete(multiples, num)
				// ↓ココ
				if (num+factor)%3 == 0 {
					k = 1
				}
			} else {
				factor = num
			}

			// ↓ココ
			for newNum := num + (factor << k); ; newNum += factor << k {
				_, hasNewFactor := multiples[newNum]
				if !hasNewFactor {
					multiples[newNum] = factor
					break
				}
				k ^= 3 // ←ココ
			}

			if !hasFactor {
				return num
			}
		}
	}

	return &primeGenerator
}

/* --------------------------------------------------------------------------
 *  debug
 * ------------------------------------------------------------------------- */
func debugln(s ...interface{}) {
	if !isProduct {
		fmt.Println(s...)
	}
}

func debug(s ...interface{}) {
	if !isProduct {
		fmt.Print(s...)
	}
}
func debugf(f string, s ...interface{}) {
	if !isProduct {
		fmt.Printf(f, s...)
	}
}

/* --------------------------------------------------------------------------
 *  std.io
 * ------------------------------------------------------------------------- */
var nextReader func() string

func newScanner() func() string {
	r := bufio.NewScanner(os.Stdin)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)
	return func() string {
		r.Scan()
		return r.Text()
	}
}
func nextString() string {
	return nextReader()
}
func nextInt64() int64 {
	v, _ := strconv.ParseInt(nextReader(), 10, 64)
	return v
}
func nextInt() int {
	v, _ := strconv.Atoi(nextReader())
	return v
}
func nextInts(n int) []int {
	r := make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt()
	}
	return r
}
func nextInt64s(n int) []int64 {
	r := make([]int64, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt64()
	}
	return r
}
func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(nextReader(), 64)
	return f
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

/* --------------------------------------------------------------------------
 *  memo
 * ------------------------------------------------------------------------- */