Language: C++

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cctype>
#include <algorithm>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template<class T>inline T next_num(){
	T i=0;char c;
	while(!isdigit(c=getchar())&&c!='-');
	bool neg=c=='-';
	neg?c=getchar():0;
	while(i=i*10-'0'+c,isdigit(c=getchar()));
	return neg?-i:i;
}
template<class T1,class T2>inline void apmax(T1 &a,const T2 &b){if(a<b)a=b;}
template<class T1,class T2>inline void apmin(T1 &a,const T2 &b){if(b<a)a=b;}
template<class T>inline void mset(T a,int v,int n){memset(a,v,n*sizeof(a[0]));}
const int N=100010;
int lend[N],rend[N];
int lst1[N],lst2[N];
template<int sat[]>inline bool sat_cmp(int a,int b){
	return sat[a]<sat[b];
}
bool vis[N];
inline int walk(int n,bool r){
	int x=0,ans=0;
	int i=1,j=n;
	mset(vis+1,0,n);
	for(int tt=n;tt--;r^=1){
		int p;
		if(r){
			for(;vis[lst1[i]];i++);
			p=lst1[i];
		}else{
			for(;vis[lst2[j]];j--);
			p=lst2[j];
		}
		vis[p]=true;
		if(x<lend[p]){
			ans+=lend[p]-x;
			x=lend[p];
		}else if(x>rend[p]){
			ans+=x-rend[p];
			x=rend[p];
		}
	}
	if(x>=0){
		ans+=x;
	}else{
		ans+=0-x;
	}
	return ans;
}
int main(){
	const int n=ni;
	for(int i=1;i<=n;i++){
		lend[i]=ni,rend[i]=ni;
		lst1[i]=lst2[i]=i;
	}
	sort(lst1+1,lst1+n+1,sat_cmp<lend>);
	sort(lst2+1,lst2+n+1,sat_cmp<rend>);
	printf("%d\n",max(walk(n,0),walk(n,1)));
	return 0;
}


Compiler output:
a.cc: In function 'int walk(int, bool)':
a.cc:46:28: error: reference to 'rend' is ambiguous
   46 |                 }else if(x>rend[p]){
      |                            ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 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:23:13: note:                 'int rend [100010]'
   23 | int lend[N],rend[N];
      |             ^~~~
a.cc:47:32: error: reference to 'rend' is ambiguous
   47 |                         ans+=x-rend[p];
      |                                ^~~~
/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:23:13: note:                 'int rend [100010]'
   23 | int lend[N],rend[N];
      |             ^~~~
a.cc:48:27: error: reference to 'rend' is ambiguous
   48 |                         x=rend[p];
      |                           ^~~~
/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:23:13: note:                 'int rend [100010]'
   23 | int lend[N],rend[N];
      |             ^~~~
a.cc: In function 'int main()':
a.cc:61:28: error: reference to 'rend' is ambiguous
   61 |                 lend[i]=ni,rend[i]=ni;
      |                            ^~~~
/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:23:13: note:                 'int rend [100010]'
   23 | int lend[N],rend[N];
      |             ^~~~
a.cc:65:30: error: parse error in template argument list
   65 |         sort(lst2+1,lst2+n+1,sat_cmp<rend>);
      |                              ^~~~~~~~~~~~~
a.cc:65:13: error: no matching function for call to 'sort(int*, int*, <unresolved overloaded function type>)'
   65 |         sort(lst2+1,lst2+n+1,sat_cmp<rend>);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:6:
/usr/include/c++/14/bits/stl_algo.h:4762:5: note: candidate: 'template<class _RAIter> void std::sort(_RAIter, _RAIter)'
 4762 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4762:5: note:   candidate expects 2 arguments, 3 provided
/usr/include/c++/14/bits/stl_algo.h:4793:5: note: candidate: 'template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)'
 4793 |     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
      |     ^~~~
/usr/include/c++/14/bits/stl_algo.h:4793:5: note:   template argument deduction/substitution failed:
a.cc:65:13: note:   couldn't deduce template parameter '_Compare'
   65 |         sort(lst2+1,lst2+n+1,sat_cmp<rend>);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:86:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:292:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator, _Compare)'
  292 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
      | ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:292:1: note:   candidate expects 4 arguments, 3 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note: candidate: 'template<class _ExecutionPolicy, class _RandomAccessIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator)'
  296 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
      | ^~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/14/pstl/glue_algorithm_defs.h:15:
/usr/include/c++/14/pstl/execution_defs.h: In substitution of 'template<class _ExecPolicy, class _Tp> using __pstl::__internal::__enable_if_execution_policy = typename std::enable_if<__pstl::execution::v1::is_execution_policy<typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type>::value, _Tp>::type [with _ExecPolicy = int*; _Tp = void]':
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1:   required by substitution of 'template<class _ExecutionPolicy, class _RandomAccessIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::sort(_ExecutionPolicy&&, _RandomAccessIterator, _RandomAccessIterator) [with _ExecutionPolicy = int*; _RandomAccessIterator = int*]'
a.cc:65:6:   required from here
   65 |         sort(lst2+1,lst2+n+1,sat_cmp<rend>);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/pstl/execution_defs.h:82:7: error: no type named 'type' in 'struct std::enable_if<false, void>'
   82 | using __enable_if_execution_policy =
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~