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

#TP0000010. Dãy Kalindrome (Kalindrome Array)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dãy Kalindrome (Kalindrome Array)

Kalindrome Array

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

An integer array is a palindrome if its elements read the same forward and backward; an empty array is also a palindrome. An array is a kalindrome if it is possible to choose one integer value xx and remove any number of elements equal to xx so that the remaining array becomes a palindrome.

Deleting zero occurrences is allowed, and deleting all occurrences is not required. Elements different from the chosen value xx cannot be deleted. Determine whether the given array of length nn is a kalindrome.

Input

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

Output

Print YES if choosing one value and deleting only its occurrences can produce a palindrome; otherwise print NO. Letter case is not significant.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n; values may repeat.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le 5000; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n; values may repeat.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le 2\cdot10^5; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n; values may repeat.

Examples

Input

5
1 4 4 1 4

Output

YES

Explanation

Choose value 4 and delete its last occurrence. The resulting array 1,4,4,1 is a palindrome.