Submission #1270046

#TimeUsernameProblemLanguageResultExecution timeMemory
1270046pramad712Hacker (BOI15_hac)C++17
100 / 100
35 ms6216 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int length;
    cin >> length;

    vector<int> values(length);
    for (int index = 0; index < length; index++) cin >> values[index];

    int window_size = (length + 1) / 2;

    vector<int> window_totals(length);

    int total = 0;

    for (int index = 0; index < window_size - 1; index++) {
        total += values[index];
    }

    for (int index = 0; index < length; index++) {
        total += values[(index + window_size - 1) % length];
        window_totals[index] = total;
        total -= values[index];
    }

    int maxima = -1;
    deque<pair<int, int>> window;

    for (int index = length - window_size + 1; index < length; index++) {
        int value = window_totals[index];

        while (!window.empty() && window.back().first > value) {
            window.pop_back();
        }

        window.emplace_back(value, index);
    }

    for (int index = 0; index < length; index++) {
        int value = window_totals[index];

        while (!window.empty() && window.back().first > value) {
            window.pop_back();
        }

        window.emplace_back(value, index);
        maxima = max(maxima, window.front().first);

        if (window.front().second == (index - window_size + 1 + length) % length) {
            window.pop_front();
        }
    }

    cout << maxima << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...