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 JavaScript (node.js v5.12) (judge verdict: Time Limit Exceeded)

/******/ (function(modules) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/
/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId]) {
/******/ 			return installedModules[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ 		// Flag the module as loaded
/******/ 		module.l = true;
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/******/
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/
/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ 		}
/******/ 	};
/******/
/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = function(exports) {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};
/******/
/******/ 	// create a fake namespace object
/******/ 	// mode & 1: value is a module id, require it
/******/ 	// mode & 2: merge all properties of value into the ns
/******/ 	// mode & 4: return value when already ns object
/******/ 	// mode & 8|1: behave like require
/******/ 	__webpack_require__.t = function(value, mode) {
/******/ 		if(mode & 1) value = __webpack_require__(value);
/******/ 		if(mode & 8) return value;
/******/ 		if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ 		var ns = Object.create(null);
/******/ 		__webpack_require__.r(ns);
/******/ 		Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ 		if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ 		return ns;
/******/ 	};
/******/
/******/ 	// getDefaultExport function for compatibility with non-harmony modules
/******/ 	__webpack_require__.n = function(module) {
/******/ 		var getter = module && module.__esModule ?
/******/ 			function getDefault() { return module['default']; } :
/******/ 			function getModuleExports() { return module; };
/******/ 		__webpack_require__.d(getter, 'a', getter);
/******/ 		return getter;
/******/ 	};
/******/
/******/ 	// Object.prototype.hasOwnProperty.call
/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";
/******/
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = "./src/ABC159/D.js");
/******/ })
/************************************************************************/
/******/ ({

/***/ "./src/ABC159/D.js":
/*!*************************!*\
  !*** ./src/ABC159/D.js ***!
  \*************************/
/*! no exports provided */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ \"fs\");\n/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(fs__WEBPACK_IMPORTED_MODULE_0__);\n\n\nfunction permutation(n, r) {\n  for (var i = 0, res = 1; i < r; i++) {\n    res *= n - i;\n  }\n\n  return res;\n}\n\nfunction combination(n, r) {\n  return permutation(n, r) / permutation(r, r);\n} // const permutation = (nums, k) => {\n//   let ans = [];\n//   if (nums.length < k) {\n//     return [];\n//   }\n//   if (k === 1) {\n//     for (let i = 0; i < nums.length; i++) {\n//       ans[i] = [nums[i]];\n//     }\n//   } else {\n//     for (let i = 0; i < nums.length; i++) {\n//       let parts = nums.slice(0);\n//       parts.splice(i, 1)[0];\n//       let row = permutation(parts, k - 1);\n//       for (let j = 0; j < row.length; j++) {\n//         if (nums[i] === row[j][0]) {\n//           ans.push([nums[i]].concat(row[j]));\n//         }\n//       }\n//     }\n//   }\n//   return ans.length;\n// };\n// const combination = (nums, k) => {\n//   let counter = 0;\n//   for (let i = 0; i < nums.length; i++) {\n//     for (let j = i + 1; j < nums.length; j++) {\n//       if (nums[i] !== nums[j]) continue;\n//       counter++;\n//     }\n//   }\n//   return counter;\n// };\n\n\nvar main = function main(input) {\n  var lines = input.split('\\n');\n  var N = lines[0];\n  var cccc = {};\n  var As = lines[1].split(' ').map(function (i) {\n    return i - 0;\n  });\n\n  for (var i = 0; i < As.length; i++) {\n    var Bs = As.slice();\n    Bs.splice(i, 1);\n\n    if (cccc[Bs.sort().join()]) {\n      console.log(cccc[Bs.sort().join()]);\n      continue;\n    }\n\n    var cache = {};\n\n    for (var j = 0, jl = Bs.length; j < jl; j++) {\n      if (cache[Bs[j]]) {\n        cache[Bs[j]]++;\n      } else {\n        cache[Bs[j]] = 1;\n      }\n    }\n\n    var sum = 0;\n\n    for (var _i = 0, _Object$keys = Object.keys(cache); _i < _Object$keys.length; _i++) {\n      var e = _Object$keys[_i];\n      sum += combination(cache[e], 2);\n    }\n\n    cccc[Bs.sort().join()] = sum;\n    console.log(sum);\n  }\n};\n\nmain(fs__WEBPACK_IMPORTED_MODULE_0__[\"readFileSync\"]('/dev/stdin', 'utf8'));\n\n//# sourceURL=webpack:///./src/ABC159/D.js?");

/***/ }),

/***/ "fs":
/*!*********************!*\
  !*** external "fs" ***!
  \*********************/
/*! no static exports found */
/***/ (function(module, exports) {

eval("module.exports = require(\"fs\");\n\n//# sourceURL=webpack:///external_%22fs%22?");

/***/ })

/******/ });