Language: C++

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;

template <class C>
struct SegmentTree {
    struct Node {
        Node operator+(const Node& o) const {
            return *this < o ? *this : o;
        }
        bool operator<(const Node& o) const {
            return C()(v, o.v);
        }
        int v, i;
    };
    SegmentTree(int n, Node& o) : n(n) {
        for (m = 2; m < n; m *= 2);
        data = vector<Node>(m + n, o);
    }
    void construct() {
        int l = m + n;
        for (int k = m; k > 1; k /= 2) {
            l = (l + 1) / 2;
            for (int i = k / 2; i < l; i++) {
                data[i] = data[i * 2] + data[i * 2 + 1];
            }
        }
    }
    void update(int i, Node& o) {
        for (data[i += m] = o; i /= 2, i > 0;) data[i] = data[i * 2] + data[i * 2 + 1];
    }
    vector<Node> data;
    int n, m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    using N0 = SegmentTree<less<>>::Node;
    using N1 = SegmentTree<greater<>>::Node;
    SegmentTree<less<>> st0(n * 2, N0{ 1 << 30, 0 });
    SegmentTree<greater<>> st1(n * 2, N1{ 0, 0 });
    vector<int64_t> sum(n + 1);
    vector<int> a(n);

    int64_t s0 = 0;
    for (int i = 0; i < n; i++) {
        int j = i + st0.m;
        cin >> st0.data[j].v;
        st0.data[j].i = i;
        s0 += st0.data[j].v;
    }
    st0.construct();

    sum[0] = s0;
    for (int k = n; k < n * 2; k++) {
        cin >> a[k - n];
        int64_t d = a[k - n] - st0.data[1].v;
        if (d > 0) {
            s0 += d;
            st0.update(st0.data[1].i, N0{ 1 << 30, 0 });
            st0.update(k, N0{ a[k - n], k });
        }
        sum[k - n + 1] = s0;
    }

    int64_t s1 = 0;
    for (int i = 0; i < n; i++) {
        int j = i + st0.m;
        cin >> st1.data[j].v;
        st1.data[j].i = i;
        s1 += st1.data[j].v;
    }
    st1.construct();

    int64_t r = sum[n] - s1;
    for (int k = n * 2 - 1; k >= n; k--) {
        int64_t d = a[k - n] - st1.data[1].v;
        if (d < 0) {
            s1 += d;
            st1.update(st1.data[1].i, N1{ 0, 0 });
            st1.update(k, N1{ a[k - n], k });
        }
        r = max(r, sum[k - n] - s1);
    }

    cout << r << endl;

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:51:36: error: cannot bind non-const lvalue reference of type 'SegmentTree<std::less<void> >::Node&' to an rvalue of type 'N0' {aka 'SegmentTree<std::less<void> >::Node'}
   51 |     SegmentTree<less<>> st0(n * 2, N0{ 1 << 30, 0 });
      |                                    ^~~~~~~~~~~~~~~~
a.cc:22:30: note:   initializing argument 2 of 'SegmentTree<C>::SegmentTree(int, Node&) [with C = std::less<void>]'
   22 |     SegmentTree(int n, Node& o) : n(n) {
      |                        ~~~~~~^
a.cc:52:39: error: cannot bind non-const lvalue reference of type 'SegmentTree<std::greater<void> >::Node&' to an rvalue of type 'N1' {aka 'SegmentTree<std::greater<void> >::Node'}
   52 |     SegmentTree<greater<>> st1(n * 2, N1{ 0, 0 });
      |                                       ^~~~~~~~~~
a.cc:22:30: note:   initializing argument 2 of 'SegmentTree<C>::SegmentTree(int, Node&) [with C = std::greater<void>]'
   22 |     SegmentTree(int n, Node& o) : n(n) {
      |                        ~~~~~~^
a.cc:71:39: error: cannot bind non-const lvalue reference of type 'SegmentTree<std::less<void> >::Node&' to an rvalue of type 'N0' {aka 'SegmentTree<std::less<void> >::Node'}
   71 |             st0.update(st0.data[1].i, N0{ 1 << 30, 0 });
      |                                       ^~~~~~~~~~~~~~~~
a.cc:35:30: note:   initializing argument 2 of 'void SegmentTree<C>::update(int, Node&) [with C = std::less<void>]'
   35 |     void update(int i, Node& o) {
      |                        ~~~~~~^
a.cc:72:27: error: cannot bind non-const lvalue reference of type 'SegmentTree<std::less<void> >::Node&' to an rvalue of type 'N0' {aka 'SegmentTree<std::less<void> >::Node'}
   72 |             st0.update(k, N0{ a[k - n], k });
      |                           ^~~~~~~~~~~~~~~~~
a.cc:35:30: note:   initializing argument 2 of 'void SegmentTree<C>::update(int, Node&) [with C = std::less<void>]'
   35 |     void update(int i, Node& o) {
      |                        ~~~~~~^
a.cc:91:39: error: cannot bind non-const lvalue reference of type 'SegmentTree<std::greater<void> >::Node&' to an rvalue of type 'N1' {aka 'SegmentTree<std::greater<void> >::Node'}
   91 |             st1.update(st1.data[1].i, N1{ 0, 0 });
      |                                       ^~~~~~~~~~
a.cc:35:30: note:   initializing argument 2 of 'void SegmentTree<C>::update(int, Node&) [with C = std::greater<void>]'
   35 |     void update(int i, Node& o) {
      |                        ~~~~~~^
a.cc:92:27: error: cannot bind non-const lvalue reference of type 'SegmentTree<std::greater<void> >::Node&' to an rvalue of type 'N1' {aka 'SegmentTree<std::greater<void> >::Node'}
   92 |             st1.update(k, N1{ a[k - n], k });
      |                           ^~~~~~~~~~~~~~~~~
a.cc:35:30: note:   initializing argument 2 of 'void SegmentTree<C>::update(int, Node&) [with C = std::greater<void>]'
   35 |     void update(int i, Node& o) {
      |                        ~~~~~~^