This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
struct segtree {
    using T = long long;
    using F = long long;
    T e() {
        return (long long) 7e18;
    }
    F id() {
        return 0;
    }
    T op(T a, T b) {
        return min(a, b);
    }
    T mapping(F f, T x) {
        return f + x;
    }
    F composition(F f, F g) {
        return f + g;
    }
    int n;
    int size;
    int log_size;
    vector<T> node;
    vector<F> lazy;
    segtree() : segtree(0) {}
    segtree(int _n) {
        build(vector<T>(_n, e()));
    }
    segtree(const vector<T>& v) {
        build(v);
    }
    void build(const vector<T>& v) {
        n = (int) v.size();
        if (n <= 1) {
            log_size = 0;
        } else {
            log_size = 32 - __builtin_clz(n - 1);
        }
        size = 1 << log_size;
        node.resize(2 * size, e());
        lazy.resize(size, id());
        for (int i = 0; i < n; i++) {
            node[i + size] = v[i];
        }
        for (int i = size - 1; i > 0; i--) {
            pull(i);
        }
    }
    void push(int x) {
        node[2 * x] = mapping(lazy[x], node[2 * x]);
        node[2 * x + 1] = mapping(lazy[x], node[2 * x + 1]);
        if (2 * x < size) {
            lazy[2 * x] = composition(lazy[x], lazy[2 * x]);
            lazy[2 * x + 1] = composition(lazy[x], lazy[2 * x + 1]);
        }
        lazy[x] = id();
    }
    void pull(int x) {
        node[x] = op(node[2 * x], node[2 * x + 1]);
    }
    void set(int p, T v) {
        assert(0 <= p && p < n);
        p += size;
        for (int i = log_size; i >= 1; i--) {
            push(p >> i);
        }
        node[p] = v;
        for (int i = 1; i <= log_size; i++) {
            pull(p >> i);
        }
    }
    T get(int p) {
        assert(0 <= p && p < n);
        p += size;
        for (int i = log_size; i >= 1; i--) {
            push(p >> i);
        }
        return node[p];
    }
    T get(int l, int r) {
        assert(0 <= l && l <= r && r <= n);
        l += size;
        r += size;
        for (int i = log_size; i >= 1; i--) {
            if (((l >> i) << i) != l) {
                push(l >> i);
            }
            if (((r >> i) << i) != r) {
                push((r - 1) >> i);
            }
        }
        T vl = e();
        T vr = e();
        while (l < r) {
            if (l & 1) {
                vl = op(vl, node[l++]);
            }
            if (r & 1) {
                vr = op(node[--r], vr);
            }
            l >>= 1;
            r >>= 1;
        }
        return op(vl, vr);
    }
    void apply(int p, F f) {
        assert(0 <= p && p < n);
        p += size;
        for (int i = log_size; i >= 1; i--) {
            push(p >> i);
        }
        node[p] = mapping(f, node[p]);
        for (int i = 1; i <= log_size; i++) {
            pull(p >> i);
        }
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= n);
        l += size;
        r += size;
        for (int i = log_size; i >= 1; i--) {
            if (((l >> i) << i) != l) {
                push(l >> i);
            }
            if (((r >> i) << i) != r) {
                push((r - 1) >> i);
            }
        }
        int ll = l;
        int rr = r;
        while (l < r) {
            if (l & 1) {
                node[l] = mapping(f, node[l]);
                if (l < size) {
                    lazy[l] = composition(f, lazy[l]);
                }
                l++;
            }
            if (r & 1) {
                r--;
                node[r] = mapping(f, node[r]);
                if (l < size) {
                    lazy[r] = composition(f, lazy[r]);
                }
            }
            l >>= 1;
            r >>= 1;
        }
        l = ll;
        r = rr;
        for (int i = 1; i <= log_size; i++) {
            if (((l >> i) << i) != l) {
                pull(l >> i);
            }
            if (((r >> i) << i) != r) {
                pull((r - 1) >> i);
            }
        }
    }
};
long long min_total_length(vector<int> a, vector<int> b) {
    int n = (int) a.size();
    int m = (int) b.size();
    a.insert(a.end(), b.begin(), b.end());
    vector<int> order(n + m);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) {
        return a[i] < a[j];
    });
    const long long inf = (long long) 1e18;
    vector<long long> dp(n + m + 1, inf);
    dp[0] = 0;
    vector<int> c;
    c.emplace_back(0);
    for (int i = 1; i < n + m; i++) {
        if ((order[i - 1] < n) != (order[i] < n)) {
            c.emplace_back(i);
        }
    }
    c.emplace_back(n + m);
    debug(c);
    segtree seg(n + m + 1);
    for (int it = 1; it < (int) c.size() - 1; it++) {
        long long sum = 0;
        for (int i = c[it]; i > c[it - 1]; i--) {
            sum += a[order[i - 1]];
            long long t = min(dp[i], dp[i - 1]);
            t -= sum;
            t += (c[it] - i) * 1LL * a[order[c[it]]];
            seg.set(i, t);
        }
        sum = 0;
        for (int i = c[it]; i < c[it + 1]; i++) {
            sum += a[order[i]];
            dp[i + 1] = min(dp[i + 1], seg.get(c[it - 1] + 1, c[it] + 1) + sum);
            if (c[it - 1] + 1 <= 2 * c[it] - i) {
                seg.apply(c[it - 1] + 1, 2 * c[it] - i, -a[order[c[it]]]);
            }
            seg.apply(max(2 * c[it] - i, c[it - 1] + 1), c[it] + 1, -a[order[c[it] - 1]]);
        }
    }
    return dp[n + m];
}
#ifdef tabr
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    debug(min_total_length({1, 2, 3, 7}, {0, 4, 5, 9, 10}));
    return 0;
}
#endif
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |