// original code https://yukicoder.me/submissions/666144 by vkgainz // hayaku naruka na? #include using namespace std; int dp[505][505]; int main() { int N; cin >> N; string S; cin >> S; #if 1 // added by tails { string T; T.reserve(N); for(char c:S){ int l=T.size(); if(c=='o'&&l>=1&&T[l-1]==c){ continue; } if(c=='n'){ if(l>=2&&T[l-2]=='p'&&T[l-1]=='o'){ T.resize(l-=2); dp[l+1][l+1]+=1; c='x'; } else if(l>=3&&T[l-3]=='p'&&T[l-2]+T[l-1]=='o'+'x'){ T.resize(l-=3); dp[l+1][l+1]=dp[l+2][l+2]+dp[l+3][l+3]+1; dp[l+2][l+2]=0; dp[l+3][l+3]=0; c='x'; } } T+=c; } S=T; N=S.size(); } #endif for(int len = 2; len <= N; len++) { for(int i = 1; i + len - 1 <= N; i++) { int j = i + len - 1; dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]); if(S[i - 1] == 'p' && S[j - 1] == 'n') { for(int k = i + 1; k < j; k++) if(S[k - 1] == 'o') dp[i][j] = max(dp[i][j], 1 + dp[i + 1][k - 1] + dp[k + 1][j - 1]); } for(int k = i; k <= j; k++) { dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]); } } } int ans = -1; for(int i = 1; i <= N; i++) { if(dp[1][i] >= 1 && dp[i + 1][N] >= 1) ans = max(ans, dp[1][i] + dp[i + 1][N] - 2); } cout << ans << "\n"; }