PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000284. Tổng khoảng cách theo giá trị (Sum of Distances)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng khoảng cách theo giá trị (Sum of Distances)

Sum of Distances

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer sequence a1,…,ana_1,\ldots,a_n, calculate bi=∑j≠i, aj=ai∣i−j∣b_i=\sum_{j\ne i,\ a_j=a_i}|i-j| for every index ii. If no other index carries the same value, bi=0b_i=0. Output the results in original index order.

Input

First line: integer nn. Second line: exactly nn integers a1,…,ana_1,\ldots,a_n.

Output

Print nn integers b1,…,bnb_1,\ldots,b_n on one line, separated by spaces.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 0≤ai≤10000000000\le a_i\le1000000000.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le100000; 0≤ai≤10000000000\le a_i\le1000000000; all elements are pairwise distinct.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le100000; 0≤ai≤10000000000\le a_i\le1000000000.

Example 1

Input

5
1 3 1 1 2

Output

5 0 3 4 0

Explanation

Value 1 occurs at positions 1, 3, 4, giving 5, 3, 4. The other values occur once.

Example 2

Input

3
0 5 3

Output

0 0 0

Explanation

All values are distinct, so every answer is zero.