#include const int inf = -(1 << 30); void chmax(int* a, int b) { if (*a < b) *a = b; } int main() { int N; char S[501]; scanf("%d", &N); scanf("%s", S); int i, j, k, l, m, dp[501][501][6], ans = -1, max[2]; for (i = 0; i < N; i++) for (j = 0; j < N; j++) for (k = 0; k < 6; k++) dp[i][j][k] = inf; for (i = 0; i < N; i++) { dp[i][i][0] = 0; if (S[i] == 'p') dp[i][i][1] = 0; else if (S[i] == 'o') dp[i][i][2] = 0; else dp[i][i][3] = 0; } for (l = 1; l < N; l++) { for (i = 0; i < N - l; i++) { j = i + l; for (k = 0; k < 6; k++) { chmax(&(dp[i][j][k]), dp[i+1][j][k]); chmax(&(dp[i][j][k]), dp[i][j-1][k]); } if (S[i] == 'p') { chmax(&(dp[i][j][0]), dp[i+1][j][5] + 1); chmax(&(dp[i][j][1]), dp[i+1][j][0]); chmax(&(dp[i][j][4]), dp[i+1][j][2]); } else if (S[i] == 'o') { chmax(&(dp[i][j][2]), dp[i+1][j][0]); chmax(&(dp[i][j][5]), dp[i+1][j][3]); } else { chmax(&(dp[i][j][3]), dp[i+1][j][0]); } if (S[j] == 'p') { chmax(&(dp[i][j][1]), dp[i][j-1][0]); } else if (S[j] == 'o') { chmax(&(dp[i][j][2]), dp[i][j-1][0]); chmax(&(dp[i][j][4]), dp[i][j-1][1]); } else { chmax(&(dp[i][j][0]), dp[i][j-1][4] + 1); chmax(&(dp[i][j][3]), dp[i][j-1][0]); chmax(&(dp[i][j][5]), dp[i][j-1][2]); } for (m = i; m < j; m++) { chmax(&(dp[i][j][0]), dp[i][m][0] + dp[m+1][j][0]); chmax(&(dp[i][j][0]), dp[i][m][1] + dp[m+1][j][5] + 1); chmax(&(dp[i][j][0]), dp[i][m][4] + dp[m+1][j][3] + 1); chmax(&(dp[i][j][1]), dp[i][m][0] + dp[m+1][j][1]); chmax(&(dp[i][j][1]), dp[i][m][1] + dp[m+1][j][0]); chmax(&(dp[i][j][2]), dp[i][m][0] + dp[m+1][j][2]); chmax(&(dp[i][j][2]), dp[i][m][3] + dp[m+1][j][0]); chmax(&(dp[i][j][3]), dp[i][m][0] + dp[m+1][j][3]); chmax(&(dp[i][j][3]), dp[i][m][3] + dp[m+1][j][0]); chmax(&(dp[i][j][4]), dp[i][m][0] + dp[m+1][j][4]); chmax(&(dp[i][j][4]), dp[i][m][1] + dp[m+1][j][2]); chmax(&(dp[i][j][4]), dp[i][m][4] + dp[m+1][j][0]); chmax(&(dp[i][j][5]), dp[i][m][0] + dp[m+1][j][5]); chmax(&(dp[i][j][5]), dp[i][m][2] + dp[m+1][j][3]); chmax(&(dp[i][j][5]), dp[i][m][5] + dp[m+1][j][0]); } chmax(&(dp[i][j][1]), dp[i][j][4]); chmax(&(dp[i][j][2]), dp[i][j][4]); chmax(&(dp[i][j][2]), dp[i][j][5]); chmax(&(dp[i][j][3]), dp[i][j][5]); chmax(&(dp[i][j][0]), dp[i][j][1]); chmax(&(dp[i][j][0]), dp[i][j][2]); chmax(&(dp[i][j][0]), dp[i][j][3]); } } for (i = 0; i < N - 1; i++) if (dp[0][i][0] > 0 && dp[i+1][N-1][0] > 0) chmax(&ans, dp[0][i][0] + dp[i+1][N-1][0] - 2); printf("%d\n", ans); fflush(stdout); return 0; }