Submission #1132097

#TimeUsernameProblemLanguageResultExecution timeMemory
1132097lopkus스탬프 수집 (JOI16_ho_t2)C++20
50 / 100
2096 ms1860 KiB
#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;
    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];
            }
        }
        anso += prefj[i - 1] * sufi[i];
        for(int j = i - 1; j >= 1; j--) {
            if(a[j] == 'O') {
                ansi += prefj[j - 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...