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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class CFC {

    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;
    final long MOD = 1000L * 1000L * 1000L + 7;
    int[] dx = {0, -1, 0, 1};
    int[] dy = {1, 0, -1, 0};
    private static final String yes = "YES";
    private static final String no = "NO";

    void solve() throws IOException {
        int n = nextInt();
        int[] arr = nextIntArr(n);
        int[][] dp11 = new int[n][n];
        int[][] dp12 = new int[n][n];
        int[][] dp21 = new int[n][n];
        int[][] dp22 = new int[n][n];
        for (int l = 1; l <= n; l++) {
            for (int i = 0; i < n; i++) {
                int j = i + l - 1;
                if (j < n) {
                    if (l == 1) {
                        if (arr[i] == 1) {
                            dp11[i][j] = 1;
                        }
                        else {
                            dp22[i][j] = 1;
                        }
                    }
                    else {
                        if (arr[i] == 1) {
                            dp11[i][j] = 1 + dp11[i + 1][j];
                            dp22[i][j] = dp22[i + 1][j];
                        }
                        else {
                            dp22[i][j] = 1 + dp22[i + 1][j];
                            dp11[i][j] = dp11[i + 1][j];
                        }
                    }
                }
            }
        }

        for (int l = 1; l <= n; l++) {
            for (int i = 0; i < n; i++) {
                int j = i + l - 1;
                if (j < n) {
                    if (l == 1) {
                        dp12[i][j] = 1;
                        dp21[i][j] = 1;
                    }
                    else {
                        if (arr[i] == 1) {
                            dp12[i][j] = Math.max(dp12[i][j], 1 + dp12[i + 1][j]);
                            dp21[i][j] = Math.max(dp21[i][j], dp21[i + 1][j]);
                        }
                        else {
                            dp21[i][j] = Math.max(dp21[i][j], 1 + dp21[i + 1][j]);
                            dp12[i][j] = Math.max(dp12[i][j], dp12[i + 1][j]);
                        }

                        if (arr[j] == 1) {
                            dp21[i][j] = Math.max(dp21[i][j], 1 + dp21[i][j - 1]);
                            dp12[i][j] = Math.max(dp12[i][j], dp12[i][j - 1]);
                        }
                        else {
                            dp21[i][j] = Math.max(dp21[i][j], dp21[i][j - 1]);
                            dp12[i][j] = Math.max(dp12[i][j], 1 + dp12[i][j - 1]);
                        }
                    }
                }
            }
        }

        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                int tmp = 0;
                if (i > 0) {
                    tmp = dp11[0][i - 1];
                }
                tmp += dp21[i][j];
                if (j < n - 1) {
                    tmp += dp22[j + 1][n - 1];
                }
                res = Math.max(res, tmp);
            }
        }

        res = Math.max(res, dp11[0][n - 1]);
        for (int i = 0; i < n - 1; i++) {
            res = Math.max(res, dp11[0][i] + dp22[i + 1][n - 1]);
        }
        res = Math.max(res, dp22[0][n - 1]);

        out(res);
    }

    void shuffle(int[] a) {
        int n = a.length;
        for(int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n - i));
            int tmp = a[i];
            a[i] = a[r];
            a[r] = tmp;
        }
    }

    int gcd(int a, int b) {
        while(a != 0 && b != 0) {
            int c = b;
            b = a % b;
            a = c;
        }
        return a + b;
    }
    private void outln(Object o) {
        out.println(o);
    }
    private void out(Object o) {
        out.print(o);
    }
    public CFC() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }
    public static void main(String[] args) throws IOException {
        new CFC();
    }

    public long[] nextLongArr(int n) throws IOException{
        long[] res = new long[n];
        for(int i = 0; i < n; i++)
            res[i] = nextLong();
        return res;
    }
    public int[] nextIntArr(int n) throws IOException {
        int[] res = new int[n];
        for(int i = 0; i < n; i++)
            res[i] = nextInt();
        return res;
    }
    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }
    public String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}
