// DP solution: try all ordering of tools used #include #include using namespace std; const int MAX = 300000; int T; int task[MAX]; int memo[3][MAX+1]; int solve(int tool_i, int task_i, int tool[]) { if (task_i >= T || tool_i >= 3) { return 0; } int &ans = memo[tool_i][task_i]; if (ans >= 0) { return ans; } ans = 0; // try doing this task if (task[task_i] == tool[tool_i]) { ans = max(ans, solve(tool_i, task_i+1, tool) + 1); } // try skipping ans = max(ans, solve(tool_i, task_i+1, tool)); // try dropping the tool ans = max(ans, solve(tool_i+1, task_i, tool)); return ans; } int solve(int tool[]) { for (int i = 0; i < 3; i++) { fill(memo[i], memo[i]+MAX+1, -1); } return solve(0, 0, tool); } int main() { cin >> T; for (int i = 0; i < MAX; i++) { cin >> task[i]; } int tool[3] = {0, 1, 2}; int ans = 0; do { ans = max(ans, solve(tool)); } while (next_permutation(tool, tool+3)); cout << ans << endl; return 0; }