Language: C++

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 5;

bool vis[N];
int x1[N], y1[N];
ll get(int n) {
	vector<int> v(n);
	for (int i = 0; i < n; i++)
		v[i] = i;
	ll ans = 0;
	do {
		int point = 0;
		ll temp = 0;
		for (int i = 0; i < v.size(); i++) {
			int l = x1[v[i]];
			int r = y1[v[i]];
			if (point >= l && point <= r) continue;
			if (point < l) {
				temp += abs(point - l);
				point = l;
			} else if (point > r) {
				temp += abs(point - r);
				point = r;
			}
		}
		ans = max(ans, temp + abs(point));
	} while (next_permutation(v.begin(), v.end()));
	return ans;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
//	srand(time(0));
//	int t = 50;
	int n;
	cin >> n;
	set<pair<int, int> > l, r;
	for (int i = 0, a, b; i < n; i++) {
		cin >> a >> b;
		x1[i] = a, y1[i] = b;
		l.insert(make_pair(a, i));
		r.insert(make_pair(b, i));
	}
	int point = 0;
	ll ans = 0;
	for (int i = 0; i < n; i++) {
		while (l.size() && vis[l.rbegin()->second])
			l.erase(*l.rbegin());
		while (r.size() && vis[r.begin()->second])
			r.erase(*r.begin());
		if (l.size()) {
			int x = l.rbegin()->first;
			int ind = l.rbegin()->second;
			if (point < x) {
				ans += x - point;
				vis[ind] = 1;
				point = x;
				continue;
			}
		}
		if (r.size()) {
			int x = r.begin()->first;
			int ind = r.begin()->second;
			if (point > x) {
				ans += point - x;
				point = x;
				vis[ind] = 1;
				continue;
			}
		}
		ans += abs(point);
		point = 0;
		break;
	}
	ans += abs(point);
	ll ans2 = ans;
	ans = 0;
	memset(vis, 0, sizeof vis);
	for (int i = 0, a, b; i < n; i++) {
		a = x1[i], b = y1[i];
		l.insert(make_pair(a, i));
		r.insert(make_pair(b, i));
	}
	point = 0;
	ans = 0;
	for (int i = 0; i < n; i++) {
		while (l.size() && vis[l.rbegin()->second])
			l.erase(*l.rbegin());
		while (r.size() && vis[r.begin()->second])
			r.erase(*r.begin());
		if (r.size()) {
			int x = r.begin()->first;
			int ind = r.begin()->second;
			if (point > x) {
				ans += point - x;
				point = x;
				vis[ind] = 1;
				continue;
			}
		}
		if (l.size()) {
			int x = l.rbegin()->first;
			int ind = l.rbegin()->second;
			if (point < x) {
				ans += x - point;
				vis[ind] = 1;
				point = x;
				continue;
			}
		}
		ans += abs(point);
		point = 0;
		break;
	}
	cout << max(ans + abs(point), ans2) << endl;
	return 0;
}



Compiler output:
a.cc:7:16: error: 'int y1 [100005]' redeclared as different kind of entity
    7 | int x1[N], y1[N];
      |                ^
In file included from /usr/include/features.h:523,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:683,
                 from /usr/include/c++/14/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:33,
                 from a.cc:1:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:257:1: note: previous declaration 'double y1(double)'
  257 | __MATHCALL (y1,, (_Mdouble_));
      | ^~~~~~~~~~
a.cc: In function 'long long int get(int)':
a.cc:18:40: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   18 |                         int r = y1[v[i]];
      |                                        ^
a.cc:18:40: error: invalid conversion from 'double (*)(double) noexcept' to 'int' [-fpermissive]
   18 |                         int r = y1[v[i]];
      |                                        ^
      |                                        |
      |                                        double (*)(double) noexcept
a.cc: In function 'int main()':
a.cc:42:32: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   42 |                 x1[i] = a, y1[i] = b;
      |                                ^
a.cc:42:34: error: assignment of read-only location '*(y1 + ((sizetype)i))'
   42 |                 x1[i] = a, y1[i] = b;
      |                            ~~~~~~^~~
a.cc:82:36: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   82 |                 a = x1[i], b = y1[i];
      |                                    ^
a.cc:82:36: error: invalid conversion from 'double (*)(double) noexcept' to 'int' [-fpermissive]
   82 |                 a = x1[i], b = y1[i];
      |                                ~~~~^
      |                                    |
      |                                    double (*)(double) noexcept