#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<char> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
int f = 0;
vector<int> prefj(n + 1, 0);
vector<int> sufi(n + 3, 0);
for(int i = 1; i <= n; i++) {
prefj[i] = prefj[i - 1] + (a[i] == 'J');
}
for(int i = n; i >= 1; i--) {
sufi[i] = sufi[i + 1] + (a[i] == 'I');
}
for(int i = 1; i <= n; i++) {
if(a[i] != 'J') {
continue;
}
for(int j = i + 1; j <= n; j++) {
if(a[j] == 'O') {
f += sufi[j + 1];
}
}
}
int ans = 0;
vector<int> sufiksj(n + 2, 0);
for(int i = n; i >= 1; i--) {
sufiksj[i] = sufiksj[i + 1];
if(a[i] == 'O') {
sufiksj[i] += sufi[i + 1];
}
}
vector<int> prefiksi(n + 1, 0);
for(int i = 1; i <= n; i++) {
prefiksi[i] = prefiksi[i - 1];
if(a[i] == 'O') {
prefiksi[i] += prefj[i - 1];
}
}
for(int i = 1; i <= n; i++) {
int ansj = f;
int anso = f;
int ansi = f;
/*
for(int j = i; j <= n; j++) {
if(a[j] == 'O') {
ansj += sufi[j + 1];
}
}*/
ansj += sufiksj[i];
anso += prefj[i - 1] * sufi[i];
/*
for(int j = i - 1; j >= 1; j--) {
if(a[j] == 'O') {
ansi += prefj[j - 1];
}
}*/
ansi += prefiksi[i - 1];
ans = max({ans, ansj, ansi, anso});
}
int ansn = f;
for(int i = n; i >= 1; i--) {
if(a[i] == 'O') {
ansn += prefj[i - 1];
}
}
ans = max(ans, ansn);
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |