Closest Equal Pair

시간 제한1초메모리 제한2048 MB

요약
모든 부분 배열에 대해 같은 색인 가장 가까운 두 원소 사이 거리를 더한다. 색이 모두 다른 부분 배열의 점수는 0이다.
난이도

어려움10점 중 8점

유형
배열, 스택, 동적 계획법
정답자
아직 제출이 없습니다

문제

Shima has opened an art museum! He is very proud of his art museum, which is most well-known for its nanoblob exhibit featuring nn nanoblobs of various colors lined up in a row. The nanoblobs are very neatly arranged - specifically, the gap between two adjacent nanoblobs is exactly 10910^9 nanometers, or 11 meter.

One day, Jerry the museum reviewer comes in to evaluate Shima's nanoblob exhibit. His evaluation process is a little peculiar. He starts by taking several pictures of the nanoblob exhibit. He is so specific about his picture taking that the following is true:

  • If two nanoblobs are in a picture, then all nanoblobs in between them are also in the picture.
  • No two pictures contain exactly the same collection of nanoblobs.
  • It is not possible for Jerry to take another picture of the exhibit without violating one of the previous two conditions.

Having taken all of these pictures, Jerry now evaluates each picture, giving each one a score. If all of the nanoblobs in a picture are distinct colors, the score of the picture is zero. Otherwise, Jerry identifies all pairs of nanoblobs that are the same color, finds the pair that are closest together, and gives the picture a score equal to the number of meters apart that these two nanoblobs are.

Jerry doesn't have time to manually do this for every picture, so he outsources it to you, his helpful assistant. Compute the sum of the scores of all of Jerry's pictures!

입력

The first line of input contains an integer nn (1≤n≤4⋅1051\le n\le 4\cdot 10^5) --- the number of nanoblobs.

The next line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (1≤a_i≤n1\le a\_i \le n for all ii), the colors of the nanoblobs in order from left to right.

출력

Output one integer: the sum of the scores of all of Jerry's pictures.

예제2

  1. 예제 1

    입력
    5
    1 3 2 1 2
    
    예상 출력
    9
    
  2. 예제 2

    입력
    10
    1 4 2 3 5 6 7 10 9 8
    
    예상 출력
    0