A. Ilya and a Colorful Walk

A. Ilya and a Colorful Walk

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Ilya lives in a beautiful city of Chordalsk.

There are nn houses on the street Ilya lives, they are numerated from 11 to nn from left to right; the distance between every two neighboring houses is equal to 11 unit. The neighboring houses are 11 and 22, 22 and 33, ..., n−1n−1 and nn. The houses nn and 11 are not neighboring.

The houses are colored in colors c1,c2,…,cnc1,c2,…,cn so that the ii-th house is colored in the color cici. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.

Ilya wants to select two houses ii and jj so that 1≤i<j≤n1≤i<j≤n, and they have different colors: ci≠cjci≠cj. He will then walk from the house ii to the house jj the distance of (j−i)(j−i) units.

Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.

Help Ilya, find this maximum possible distance.

Input

The first line contains a single integer nn (3≤n≤3000003≤n≤300000) — the number of cities on the street.

The second line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤n1≤ci≤n) — the colors of the houses.

It is guaranteed that there is at least one pair of indices ii and jj so that 1≤i<j≤n1≤i<j≤n and ci≠cjci≠cj.

Output

Print a single integer — the maximum possible distance Ilya can walk.

Examples

input

Copy

5
1 2 3 2 3

output

Copy

4

input

Copy

3
1 2 1

output

Copy

1

input

Copy

7
1 1 3 1 1 1 1

output

Copy

4

Note

In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5−1=45−1=4 units.

In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 11 unit.

In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7−3=47−3=4 units.

思路:

分为两种情况,一种是最后一个和第一个不相等时,距离是n-1。另一种情况是不相等时,从后往前找,第一个与s[1]不相等的。再从前往后找,第一个与s[n]不相等的你,取两者较大值。

代码:

#include<set>
#include<map>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int s[300005];
int main()
{
    int n,MAX=0;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&s[i]);
    }
    if(s[n]!=s[1])//最后一个和第一个不相等时,找到
    {
        printf("%d\n",n-1);
        return 0;
    }
    else
    {
        for(int i=1; i<n; i++)//从后往前找
        {
            if(s[n]!=s[i])
            {
                MAX=n-i;
                break;
            }
        }
        for(int i=n; i>1; i--)//从前往后找,取两者最大
        {
            if(s[1]!=s[i])
            {
                if(MAX<i-1)
                {
                    MAX=i-1;
                    break;
                }
            }
        }
        printf("%d\n",MAX);
    }
    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/shinian_acmer/article/details/89066435