/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
		int size = sc.nextInt();
 
		int[] arr =new int[size];
 
		for(int i = 0;i<size;i++){
			arr[i] = sc.nextInt();
		}

		int result = maxLen(arr);
        System.out.println(result);

	}
	public static int maxLen(int[] arr) {
        
        HashMap<Integer, Integer> present = new HashMap<>();
        
        present.put(0, -1); 
        
        int sum = 0;
        int maxLongestSubArray = 0;
        
        for (int i = 0; i < arr.length; i++) {
            
            sum += (arr[i] == 0) ? -1 : 1;
            
            if (present.containsKey(sum)) {
                int currentLongestSubArray = i - present.get(sum);
                maxLongestSubArray = Math.max(currentLongestSubArray, maxLongestSubArray);
            } else {
                present.put(sum, i);
            }
        }
        
        return maxLongestSubArray;
    }
}