Language: C++

#include<cstdio>
#define ll long long
#include<algorithm>
using namespace std;
const int MAX_N=100000+5;
ll arr[MAX_N];
int next[MAX_N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%lld",&arr[i]);
    sort(arr,arr+n);
    next[0]=n-1;
    next[n-1]=1;
    int front,rear;
    front=0;
    rear=1;
    int frontT,rearT;
    frontT=n-1;
    rearT=n-1;
    int q,p;
    q=2;
    p=n-2;
    int i=0;
    while(p!=q)
    {
        if(i%2==0)
        {
            int k=abs(arr[q]-arr[front])>abs(arr[q]-arr[rear])?front:rear;
            next[q]=k;
            if(k==front)
            {
                frontT=q;
                next[q]=front;
                front=q;
            }
            else
            {
                rearT=rear;
                next[rear]=q;
                rear=q;
            }
            q++;
        }
        else
        {
            int a=abs(arr[p]-arr[front])+abs(arr[p]-arr[frontT]);
            int b=abs(arr[p]-arr[rear])+abs(arr[p]-arr[rearT]);
            if(a>b)
            {
                next[front]=p;
                next[p]=frontT;
                frontT=p;
            }
            else
            {
                next[rearT]=p;
                next[p]=rear;
                rearT=p;
            }
            p--;
        }
        i++;
    }
    int cur=front;
    int count=0;
    ll sum=0;
    printf("%lld\n",sum);
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:15:5: error: reference to 'next' is ambiguous
   15 |     next[0]=n-1;
      |     ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~
a.cc:16:5: error: reference to 'next' is ambiguous
   16 |     next[n-1]=1;
      |     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~
a.cc:32:13: error: reference to 'next' is ambiguous
   32 |             next[q]=k;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~
a.cc:36:17: error: reference to 'next' is ambiguous
   36 |                 next[q]=front;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~
a.cc:42:17: error: reference to 'next' is ambiguous
   42 |                 next[rear]=q;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~
a.cc:53:17: error: reference to 'next' is ambiguous
   53 |                 next[front]=p;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~
a.cc:54:17: error: reference to 'next' is ambiguous
   54 |                 next[p]=frontT;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~
a.cc:59:17: error: reference to 'next' is ambiguous
   59 |                 next[rearT]=p;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~
a.cc:60:17: error: reference to 'next' is ambiguous
   60 |                 next[p]=rear;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:7:5: note:                 'int next [100005]'
    7 | int next[MAX_N];
      |     ^~~~