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

#BS0000167. Xóa một đoạn để các phần tử còn lại phân biệt (Uniqueness)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Xóa một đoạn để các phần tử còn lại phân biệt (Uniqueness)

Uniqueness

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array a1,a2,…,ana_1,a_2,\ldots,a_n. You may remove at most one contiguous subarray: choose 1≤l≤r≤n1\le l\le r\le n, delete positions ll through rr, and concatenate the remaining prefix and suffix without changing their relative order.

Afterwards, all remaining values must be pairwise distinct: no two remaining positions may have the same value.

Find the minimum number of removed elements. If the original array already has distinct elements, you may remove nothing and print 0.

Input

  • The first line contains an integer nn, the array length.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

Print one integer: the minimum length of a removed contiguous subarray that makes all remaining elements distinct; print 0 if no removal is necessary.

Subtasks

  • Subtask 1 (20 points): 1≤n≤251\le n\le25; 1≤ai≤1001\le a_i\le100 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤3001\le n\le300; 1≤ai≤1061\le a_i\le10^6 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤20001\le n\le2000; 1≤ai≤1091\le a_i\le10^9 for every 1≤i≤n1\le i\le n.

Examples

Input

4
1 1 2 2

Output

2

Explanation

The initial array is [1,1,2,2][1,1,2,2]. Remove positions 22 and 33, namely the middle subarray [1,2][1,2]. The remaining array is [1,2][1,2], which has distinct values.

Removing just one element is insufficient: initially there are two duplicated values (two copies of 11 and two copies of 22), and removing one element can eliminate at most one duplicate. Thus at least 22 elements must be removed, and the output is 2.