这题其实题意是很好理解的,每个值乘以该值与其最长的两个连续长度,然后求出这样的最大值,这题的难点其实就是如何去记录最长的两个字段长度,我们在这里选择Map,其类型为<Integer,int[]> ,装的数组是一个长度为2的数组,表示最长的两个长度

然后就需要遍历数组,求出每一个连续字段的长度,然后把它装进Map中,为此我们需要先获得当前Map中装的数组,然后判断当前的长度是否长于其中的最小值,如果是,那么就把它装入数组,也就是把最小的那个替换掉,最后记得要把该数组装进map中,然后i=i+len

最后遍历map就行了,但是要注意,理论上的最大值是有可能超过int范围的,所以需要用long来装,最后输出max即可


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int a[]=new int[n+1];
		HashMap<Integer,int[]>map=new HashMap();
		for(int i=1;i<=n;i++) {
			a[i]=sc.nextInt();
		}
		for(int i=1;i<=n;) {
			int t=i;
			while(t<=n&&a[t]==a[i]) {
				t++;
			}
			int len=t-i;
			int l[]=map.getOrDefault(a[i],new int[] {0,0});
			int temp=l[0]>l[1]?1:0;
			l[temp]=len;
			map.put(a[i],l);
			i=i+len;
		}
		
		
		long max=-1;
		for(Map.Entry<Integer,int[]> entry:map.entrySet()) {
			long key=entry.getKey();
			int len[]=entry.getValue();
			long res=key*(long)(len[0]+len[1]);
			if(res>max)max=res;
		}
		System.out.println(max);
		
			
			
		
		
		

	}

}