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 Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)

module Main where

import           Debug.Trace
import qualified Data.Vector.Unboxed           as VU
import           Data.List
import qualified Data.ByteString               as BS
import qualified Data.ByteString.Char8         as BSC8
import           Data.Char
import           Data.Maybe


getVUIntFromInputLineAtBSC8 :: IO (VU.Vector Int)
getVUIntFromInputLineAtBSC8 =
    VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
getVUListIntFromInputLineAtBSC8 =
    VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
readInt = fst . fromJust . BSC8.readInt
readIntList = map readInt . BSC8.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main :: IO [()]
main = do
    --s <- getLine
    --[aM,aN] <- getListIntFromInputLineAtBSC8
    --aP                   <- getVUIntFromInputLineAtBSC8
    --aQ                   <- getVUIntFromInputLineAtBSC8
    --aR                   <- getVUIntFromInputLineAtBSC8

    --vP <- VU.fromList . take aX . reverse . sort <$> getIntList
    --vQ <- VU.fromList . take aY . reverse . sort <$> getIntList
    --vR <- VU.fromList . take (aX + aY) . reverse . sort <$> getIntList

    aN <- getInt
    aA <- getIntList

    let aAP   = [ (x - 1, 1) | x <- aA ] :: [(Int, Int)]

    let cntAP = VU.accum (+) (VU.replicate aN 0) aAP

    let cntSum = foldl (\acc1 minus -> acc1 ++ [sumCon2 minus cntAP]) [] aA
    mapM (putStrLn . show) cntSum

sumCon2 :: Int -> VU.Vector Int -> Int
sumCon2 minus cntAP =
    let sumCnt = VU.accum (+) cntAP [(minus - 1, -1)]
    in  VU.foldl (\acc x -> acc + if x > 1 then con2 x else 0) 0 sumCnt

con2 :: Int -> Int
con2 x = div (x * (x - 1)) 2

fact :: Int -> Int
fact 1 = 1
fact x = x * fact (x - 1)

judgeCombi :: Int -> Int -> VU.Vector Int -> Int -> Int
judgeCombi target n cntAP minus =
    let combiList = getCombiList target n
    in
        foldl
            (\acc1 first ->
                acc1
                    + (foldl
                          (\acc2 second ->
                              acc2
                                  + (if (judge first second minus cntAP)
                                        then 1
                                        else 0
                                    )
                          )
                          0
                          combiList
                      )
            )
            0
            combiList

getCombiList :: Int -> Int -> [(Int, Int)]
getCombiList target n
    | target == n = []
    | otherwise = foldl
        (\acc x -> acc ++ if (x < (target - x)) then [(x, target - x)] else [])
        []
        [(max 1 (target - n)) .. (min n (target - 1))]

judge :: (Int, Int) -> (Int, Int) -> Int -> VU.Vector Int -> Bool
judge z a y cntAP =
    let
        pointList =
            [ (fst z - 1, 1)
            , (snd z - 1, 1)
            , (fst a - 1, 1)
            , (snd a - 1, 1)
            , (y - 1    , 1)
            ]
        sumAP = trace (show cntAP ++ show pointList)
            $ VU.accum (-) (cntAP) pointList
        ret = VU.foldl (\acc x -> acc && (if x >= 0 then True else False))
                       True
                       sumAP
    in
        trace (show ret) $ ret