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

#PS0000283. Tổng khoảng cách giữa các phần tử giống nhau (Intervals Between Identical Elements)

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 giữa các phần tử giống nhau (Intervals Between Identical Elements)

Intervals Between Identical Elements

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; 1≤ai≤1000001\le a_i\le100000.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le100000; 1≤ai≤1000001\le a_i\le100000; all elements are pairwise distinct.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le100000; 1≤ai≤1000001\le a_i\le100000.

Example 1

Input

7
2 1 3 1 2 3 3

Output

4 2 7 2 4 4 5

Explanation

Value 2 occurs at positions 1, 5, distance 4; value 1 at 2, 4, distance 2; value 3 at 3, 6, 7, giving 7, 4, 5 respectively.

Example 2

Input

4
10 5 10 10

Output

5 0 3 4

Explanation

The positions carrying 10 are 1, 3 and 4, giving 5, 3, 4. Value 5 occurs once.