#P7527. Unique Leaders Team Selection

    ID: 20722 Type: Default 1000ms 256MiB

Unique Leaders Team Selection

Unique Leaders Team Selection

In the United Cows of Farmer John (UCFJ), there are N cows, arranged in a line. The breed of the i-th cow is given as \(b_i\). UCFJ wants to select a representative team to participate in the International bOvine olympIad (IOI). The team must consist of a contiguous segment of at least two cows, i.e. cows from positions \(l\) to \(r\) with \(1 \le l < r \le N\). The cows on the ends of the segment are designated as leaders.

In order to avoid intra-breed conflicts, each leader's breed must appear only once within the entire team (i.e. neither leader’s breed can occur anywhere else in the team, including the other leader).

Formally, if a team consists of cows at positions \(l, l+1, \dots, r\) (using 1-indexing), then it is valid if and only if:

  • The frequency of \(b_l\) in \(b_l, b_{l+1}, \dots, b_r\) is exactly 1.
  • The frequency of \(b_r\) in \(b_l, b_{l+1}, \dots, b_r\) is exactly 1.

Your task is to count the number of valid teams that can be formed.

Input Format: The first line contains an integer \(N\). The second line contains \(N\) space-separated integers \(b_1, b_2, \dots, b_N\), representing the breeds of the cows.

Output Format: Output a single integer indicating the number of valid teams.

inputFormat

The input begins with an integer \(N\) (the number of cows). The next line contains \(N\) space-separated integers \(b_1, b_2, \dots, b_N\), where \(b_i\) denotes the breed of the \(i\)-th cow.

outputFormat

Output a single integer representing the total number of valid contiguous segments (teams) meeting the leader conditions.

sample

3
1 2 3
3

</p>