#include <bits/stdc++.h>
using namespace std;

long getMaximumGrossValue(vector<int> arr) {
    int n = arr.size();

    vector<long long> pref(n + 1, 0);

    for (int i = 1; i <= n; i++)
        pref[i] = pref[i - 1] + arr[i - 1];

    vector<long long> bestPrefix(n + 1);
    bestPrefix[0] = pref[0];
    for (int i = 1; i <= n; i++)
        bestPrefix[i] = max(bestPrefix[i - 1], pref[i]);

    vector<long long> bestSuffix(n + 1);
    bestSuffix[n] = pref[n];
    for (int i = n - 1; i >= 0; i--)
        bestSuffix[i] = max(bestSuffix[i + 1], pref[i]);

    long long ans = LLONG_MIN;

    for (int b = 0; b <= n; b++) {
        long long cur = 2LL * bestPrefix[b]
                      - 2LL * pref[b]
                      + 2LL * bestSuffix[b]
                      - pref[n];
        ans = max(ans, cur);
    }

    return ans;
}

int main() {
    // Example from the problem description
    std::vector<int> arr = {-5, 3, 9, 4};
    // vector<int> arr = {-1,1,-2,-2};
    // vector<int> arr= {4,-8,2,-10,3,-20};
    std::cout << "Max Gross Value: " << getMaximumGrossValue(arr) << std::endl; // Output: 21
    return 0;
}