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>
#include "wiring.h"
using namespace std;
#define N 200100
long long dp[N];
const long long INF = 1e18;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
    vector<pair<int, int >> v;
    for (auto x:r) v.emplace_back(x, 0);
    for (auto x:b) v.emplace_back(x, 1);
    v.emplace_back(-1, -2);
    sort(v.begin(), v.end());
    dp[0] = 0;
    auto cost = [&](int l, int r) {
        vector<int> st[2];
        long long res = 0;
        for (; l <= r; l++) st[v[l].second].push_back(v[l].first);
        if (st[0].empty() || st[1].empty()) return INF;
        for (int i = 0; i < min(st[0].size(), st[1].size()); i++) {
            res += abs(st[0][i] - st[1][i]);
        }
        if (st[0].size() < st[1].size()) {
            for (int i = st[0].size(); i < st[1].size(); i++) {
                res += min(abs(st[0].back() - st[1][i]), abs(st[0].front() - st[1][i]));
            }
        } else {
            for (int i = st[1].size(); i < st[0].size(); i++) {
                res += min(abs(st[1].back() - st[0][i]), abs(st[1].front() - st[0][i]));
            }
        };
        return res;
    };
    for (int i = 1; i < (int) v.size(); i++) {
        dp[i] = INF;
        long long cnt[] = {0, 0};
        long long sum[] = {0ll, 0ll};
        for (int j = i; j > 0; j--) {
            sum[v[j].second] += v[j].first;
            cnt[v[j].second]++;
            if (v[i].second != v[j].second) {
                dp[i] = min(dp[i], dp[i - 1] + v[i].first - v[j].first);
            }
            if (min(cnt[0], cnt[1]) == 1 && v[i].second != v[j].second) {
                long long cost = 0ll;
                if (cnt[v[i].second] == 1) {
                    cost = v[i].first * cnt[v[j].second] - sum[v[j].second];
                } else {
                    cost = sum[v[i].second] - cnt[v[i].second] * v[j].first;
                }
//                cerr << i << ' ' << j << ' ' << cost << endl;
                dp[i] = min(dp[i], dp[j - 1] + cost);
            }
            dp[i] = min(dp[i], dp[j - 1] + cost(j, i));
            if (cnt[0] == cnt[1]) {
                break;
            }
        }
//        cout << dp[i] << ' ';
    }
    return dp[v.size() - 1];
}
Compilation message (stderr)
wiring.cpp: In lambda function:
wiring.cpp:23:27: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   23 |         for (int i = 0; i < min(st[0].size(), st[1].size()); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wiring.cpp:28:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |             for (int i = st[0].size(); i < st[1].size(); i++) {
      |                                        ~~^~~~~~~~~~~~~~
wiring.cpp:32:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |             for (int i = st[1].size(); i < st[0].size(); i++) {
      |                                        ~~^~~~~~~~~~~~~~| # | 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... |