A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.

A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.

Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.

A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.

Input

The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.

The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).

Output

Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.

Examples

Input

4
1 2 1 2


Output

4


Input

10
1 1 2 2 2 1 1 2 2 1


Output

9

Note

In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.

In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
import java.util.*;
import java.lang.*;

public class Main{
   public static class Interval{
      int start; 
      int end; 
      int p;
      Interval(int start, int end, int p){
          this.start = start;
          this.end = end;
          this.p = p;
      }
   }

   /*


    #include<bits/stdc++.h>
    using namespace std;
    const int N = 2005;
    int dp[N][N][2];
    int arr[N],pre[N],suf[N];
    int main(){
      int n;
      cin>>n;
      for(int i=1;i<=n;i++){
        cin>>arr[i];
        pre[i]=pre[i-1]+(arr[i]==1);
      }
      for(int i=n;i>0;i--) 
         suf[i]=suf[i+1]+(arr[i]==2);
      
      int ans=0;
      for(int i=1;i<=n;i++){
        for(int j=i;j<=n;j++){
          dp[i][j][0]=dp[i][j-1][0]+(arr[j]==2);
          dp[i][j][1]=max(dp[i][j-1][0],dp[i][j-1][1])+(arr[j]==1);
          ans=max(ans,pre[i-1]+suf[j+1]+dp[i][j][0]);
          ans=max(ans,pre[i-1]+suf[j+1]+dp[i][j][1]);
        }
      }
      cout<<ans<<endl;
      return 0;
    }


   */

    public static void main(String[] args) {
      	Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int[] nums = new int[N];
        for(int i=0;i<N;i++){
            nums[i] = scan.nextInt();
        }

        System.out.println(ReverseMaxSubsequence(nums));

    }

    public static int ReverseMaxSubsequence(int[] nums){
         int[] pre = new int[nums.length];
         int[] suffix = new int[nums.length];
         for(int i=0;i<nums.length;i++){
              if(i==0){
                 pre[i] = (nums[i] == 1)?1:0;
              }else{
                pre[i] += pre[i-1] + ((nums[i]==1)?1:0);
              }
         }

         for(int i=nums.length-1;i>=0;i--){
            if(i==nums.length-1){
               suffix[i] = (nums[i]==2)?1:0;
            }else{
               suffix[i] += suffix[i+1] + ((nums[i]==2)?1:0);
            }
         }

         
         int[][][] dp = new int[nums.length][nums.length][2];
         int ans = 0;
         for(int i=0;i<nums.length;i++){
          for(int j=i;j<nums.length;j++){
               if(i==j){
                   dp[i][j][1] = (nums[i]==2)?1:0;
                   dp[i][j][0] = (nums[i]==1)?1:0;
               }else{
                   dp[i][j][1] = dp[i][j-1][1] + (nums[j]==2?1:0);
                   dp[i][j][0] = dp[i][j-1][0];
                   if(nums[j]==1){
                     dp[i][j][0] = Math.max(dp[i][j-1][1], dp[i][j-1][0]) + 1;
                   }
               }

               int pre_ones = (i <= 0 ? 0 : pre[i-1]);
               int suffix_twos = (j>=nums.length-1?0:suffix[j+1]);
               ans = Math.max(ans, Math.max(dp[i][j][0], dp[i][j][1]) + pre_ones + suffix_twos);

          }
         }


         return ans;



    }

    public static int bookShelf(int N, int B, int[] cows){
        int sum = 0;
        for(int ele:cows){
            sum+=ele;
        }
        boolean[][] dp = new boolean[cows.length+1][sum+1];
        dp[0][0] = true;

        int dis = sum - B;
        for(int i=0;i<cows.length;i++){
            int h = cows[i];
            for(int j=0;j<=sum;j++){
                dp[i+1][j] = dp[i][j];
                if(j-h>=0){
                   dp[i+1][j] |= dp[i][j-h];
                }

                if(dp[i+1][j] == true && j>=B){
                   dis = Math.min(dis, j-B);
                }
            }
        }

        return dis;       
    }

    public static int balance(int C, int G, int[] hooks, int[] weights){
          int[][] dp = new int[G+1][15005];
          dp[0][7500] = 1;
          for(int i=0;i<weights.length;i++){
              int weight = weights[i];
              for(int j=0;j<C;j++){
                  int offset = hooks[j]*weight;
                  for(int k=0;k<15005;k++){
                       if(k+offset>=0 && k+offset<=15005 && dp[i][k]!=0){
                              dp[i+1][k+offset] += dp[i][k];
                       }
                  }

              }
          }

          return dp[G][7500];
    }

    public static int milikingTime(int N, int R, List<Interval> intervalList){
        Collections.sort(intervalList, new Comparator<Interval>(){
               public int compare(Interval a, Interval b){
                    if(a.start == b.start){
                        return a.end - b.end;
                    }
                    return a.start - b.start;
               }
        });

        int[] dp = new int[intervalList.size()];
        int res = 0;
        for(int i=0;i<intervalList.size();i++){
           Interval cur = intervalList.get(i);
           dp[i] = cur.p;
           for(int j=i-1;j>=0;j--){
               if(cur.start-R>=intervalList.get(j).end){
                    dp[i] = Math.max(dp[i], cur.p+dp[j]);
               }
           }

           res = Math.max(res, dp[i]);
        }

        return res;


    }
    public static int largestSkiLen = 1;
    public static int skingPath(int R, int C, int[][] a){
        if(R==0 || C == 0){
           return 0;
        }
        int[][] dp = new int[R][C];
        for(int[] ele:dp){
           Arrays.fill(ele, 1);
        }
        
        for(int i=0;i<R;i++){
          for(int j=0;j<C;j++){
               if(dp[i][j] >= 2){
                    continue;
               }
               dfsSearch(a, dp, i, j);
            }
        }
        
        return largestSkiLen;
    }

    public static int dfsSearch(int[][] a, int[][] dp, int i, int j){
         int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
         if(i<0 || i>=a.length || j<0 || j>=a[0].length){
             return 0;
         }
         if(dp[i][j]>=2){
            return dp[i][j];
         }

         for(int[] dir:dirs){
            int next_i = i + dir[0];
            int next_j = j + dir[1];
            if(next_i>=0 && next_i<a.length && next_j>=0 && next_j<a[0].length && a[next_i][next_j] < a[i][j]){
                 int len = 1+dfsSearch(a, dp, next_i, next_j);
                 dp[i][j] = Math.max(dp[i][j], len);
            }
         }

         largestSkiLen = Math.max(largestSkiLen, dp[i][j]);
         return dp[i][j];
    } 

    public static int solve(int T, int W, int[] nums){
          Integer[][][] dp = new Integer[T+1][W+1][3];
          return dpSearch(dp, nums, 0, W, 1);
    }


    public static int dpSearch(Integer[][][] dp, int[] nums, int i, int K, int pos){
       if(K<0 || i>=nums.length){
       	   return 0;
       }

       if(dp[i][K][pos] != null){
       	  return dp[i][K][pos];
       }

       if(K==0){
       	  int add = (pos == nums[i] ? 1 : 0);
       	  dp[i][K][pos] = dpSearch(dp, nums, i+1, K, pos) + add;
       	  return dp[i][K][pos];
       }

       int t1 = dpSearch(dp, nums, i+1, K, pos) + (pos == nums[i] ? 1 : 0);
       int t2 = dpSearch(dp, nums, i+1, K-1, 3-pos) + ((3-pos) == nums[i] ? 1 : 0);
       dp[i][K][pos] = Math.max(t1, t2);
       return dp[i][K][pos];
    }






}



















