Language: C++

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

typedef long long ll;

int N;
int L[100005], R[100005];
ll sm;
int pos;

set<pair<int,int>> lend, rend;

int main() {
	scanf("%d", &N);
	for (int i = 0; i < N; ++i) scanf("%d%d", L+i, R+i);

	for (int i = 0; i < N; ++i) lend.insert({L[i], i}), rend.insert({R[i], i});

	for (int i = 0; i < N; ++i) {
		int i1 = prev(lend.end())->second;
		int i2 = rend.begin()->second;
		if (max(L[i1] - pos, 0) > max(pos - R[i2], 0)) {
			sm += max(L[i1] - pos, 0);
			if (pos < L[i1]) pos = L[i1];
			lend.erase(prev(lend.end()));
			rend.erase(rend.find({R[i1], i1}));
		} else {
			sm += max(pos - R[i2], 0);
			if (pos > R[i2]) pos = R[i2];
			lend.erase(lend.find({L[i2], i2}));
			rend.erase(rend.begin());
		}
	}

	sm += abs(pos);

	printf("%lld\n", sm);
}

Compiler output:
a.cc: In function 'int main()':
a.cc:17:61: error: reference to 'rend' is ambiguous
   17 |         for (int i = 0; i < N; ++i) lend.insert({L[i], i}), rend.insert({R[i], i});
      |                                                             ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:227:5: note: candidates are: 'template<class _Tp> constexpr std::reverse_iterator<const _Tp*> std::rend(initializer_list<_Tp>)'
  227 |     rend(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:205:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr std::reverse_iterator<_Tp*> std::rend(_Tp (&)[_Nm])'
  205 |     rend(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:183:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(const _Container&)'
  183 |     rend(const _Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:172:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(_Container&)'
  172 |     rend(_Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
a.cc:11:26: note:                 'std::set<std::pair<int, int> > rend'
   11 | set<pair<int,int>> lend, rend;
      |                          ^~~~
a.cc:21:26: error: reference to 'rend' is ambiguous
   21 |                 int i2 = rend.begin()->second;
      |                          ^~~~
/usr/include/c++/14/bits/range_access.h:227:5: note: candidates are: 'template<class _Tp> constexpr std::reverse_iterator<const _Tp*> std::rend(initializer_list<_Tp>)'
  227 |     rend(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:205:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr std::reverse_iterator<_Tp*> std::rend(_Tp (&)[_Nm])'
  205 |     rend(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:183:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(const _Container&)'
  183 |     rend(const _Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:172:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(_Container&)'
  172 |     rend(_Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
a.cc:11:26: note:                 'std::set<std::pair<int, int> > rend'
   11 | set<pair<int,int>> lend, rend;
      |                          ^~~~
a.cc:26:25: error: reference to 'rend' is ambiguous
   26 |                         rend.erase(rend.find({R[i1], i1}));
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:227:5: note: candidates are: 'template<class _Tp> constexpr std::reverse_iterator<const _Tp*> std::rend(initializer_list<_Tp>)'
  227 |     rend(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:205:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr std::reverse_iterator<_Tp*> std::rend(_Tp (&)[_Nm])'
  205 |     rend(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:183:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(const _Container&)'
  183 |     rend(const _Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:172:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(_Container&)'
  172 |     rend(_Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
a.cc:11:26: note:                 'std::set<std::pair<int, int> > rend'
   11 | set<pair<int,int>> lend, rend;
      |                          ^~~~
a.cc:26:36: error: reference to 'rend' is ambiguous
   26 |                         rend.erase(rend.find({R[i1], i1}));
      |                                    ^~~~
/usr/include/c++/14/bits/range_access.h:227:5: note: candidates are: 'template<class _Tp> constexpr std::reverse_iterator<const _Tp*> std::rend(initializer_list<_Tp>)'
  227 |     rend(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:205:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr std::reverse_iterator<_Tp*> std::rend(_Tp (&)[_Nm])'
  205 |     rend(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:183:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(const _Container&)'
  183 |     rend(const _Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:172:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(_Container&)'
  172 |     rend(_Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
a.cc:11:26: note:                 'std::set<std::pair<int, int> > rend'
   11 | set<pair<int,int>> lend, rend;
      |                          ^~~~
a.cc:31:25: error: reference to 'rend' is ambiguous
   31 |                         rend.erase(rend.begin());
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:227:5: note: candidates are: 'template<class _Tp> constexpr std::reverse_iterator<const _Tp*> std::rend(initializer_list<_Tp>)'
  227 |     rend(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:205:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr std::reverse_iterator<_Tp*> std::rend(_Tp (&)[_Nm])'
  205 |     rend(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:183:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(const _Container&)'
  183 |     rend(const _Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:172:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(_Container&)'
  172 |     rend(_Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
a.cc:11:26: note:                 'std::set<std::pair<int, int> > rend'
   11 | set<pair<int,int>> lend, rend;
      |                          ^~~~
a.cc:31:36: error: reference to 'rend' is ambiguous
   31 |                         rend.erase(rend.begin());
      |                                    ^~~~
/usr/include/c++/14/bits/range_access.h:227:5: note: candidates are: 'template<class _Tp> constexpr std::reverse_iterator<const _Tp*> std::rend(initializer_list<_Tp>)'
  227 |     rend(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:205:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr std::reverse_iterator<_Tp*> std::rend(_Tp (&)[_Nm])'
  205 |     rend(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:183:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(const _Container&)'
  183 |     rend(const _Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:172:5: note:                 'template<class _Container> constexpr decltype (__cont.rend()) std::rend(_Container&)'
  172 |     rend(_Container& __cont) -> decltype(__cont.rend())
      |     ^~~~
a.cc:11:26: note:                 'std::set<std::pair<int, int> > rend'
   11 | set<pair<int,int>> lend, rend;
      |                          ^~~~