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.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.flush();out.close();
    }
        static class TaskE {
         public void solve(int testNumber, InputReader in, PrintWriter out) {
             int n=in.nextInt();
             int a[]=new int[n+1];
             int n1[]=new int[n+1],n2[]=new int[n+1];
             for(int i=0;i<n;i++){
              a[i]=in.nextInt();
              if(a[i]==1){
               n1[i+1]=n1[i]+1;n2[i+1]=n2[i];
              }
              else{ n2[i+1]=n2[i]+1;n1[i+1]=n1[i];}
             }
             int n12[][]=new int[n+2][n+2],n21[][]=new int[n+2][n+2];
             for(int i=1;i<=n;i++){
              for(int j=i;j<=n;j++){
               if(a[j-1]==2){
                n12[i][j]=n12[i][j-1]+1;
                n21[i][j]=Math.max(n21[i][j-1],n2[j-1]-n2[i-1]+1);
               }else{
                n12[i][j]=Math.max(n12[i][j-1],n1[j-1]-n1[i-1]+1);
                n21[i][j]=n21[i][j-1]+1;
               }
              }
             }
             int max=0;
             for(int i=1;i<=n;i++){
              for(int j=i;j<=n;j++){
               int v1=n1[j]+Math.max(n12[j+1][n],Math.max(n1[n]-n1[j],n2[n]-n2[j]));
               int v2=n2[n]-n2[i-1]+Math.max(n1[i-1],n12[1][i-1]);
               int v3=n21[i][j]+n1[i-1]+n2[n]-n2[j];
               max=Math.max(max,Math.max(v1,Math.max(v2,v3)));
               //System.out.println(max+" "+i+" "+j);
              }
             }out.print(max);
         }
    }
    static class InputReader {
        BufferedReader br;
        StringTokenizer st;
        public InputReader(InputStream stream) {
            br = new BufferedReader(new InputStreamReader(stream));
            st = null;
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}