Language: C++

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
int n;
ll a[2010];
ll sum[2010];
bool visit[2010];
vector<PLI> v;
ll res;
int main()
{
	cin >>n;
	for (ll i=0;i<n;i++)
	{
		cin >>a[i];
		v.push_back(MP(-a[i],i));
		res+=max(i,n-1-i)*a[i];
	}
	sort(v.begin(),v.end());
	for (int i=0;i<n-1;i++)
	{
		int x=v[i].Snd;
		fill(sum,sum+n,0); 
		int l=0,r=n-1,li,ri;
		while (visit[l]) l++;
		while (visit[r]) r--;
		li=l+1,ri=r-1;
		while (visit[li]) li++;
		while (visit[ri]) ri--;
		for (int j=i+1;j<n;j++) sum[(abs(v[j].Snd-l)>abs(r-v[j].Snd) ? l : r)]-=(max(abs(v[j].Snd-l),abs(r-v[j].Snd))-max(abs(v[j].Snd-li),abs(ri-v[j].Snd)))*v[j].Fst;
		/*cout <<l<<" "<<r<<" "<<li<<" "<<ri<<" ##\n";
		for (int j=0;j<n;j++) cout <<sum[j]<<" ";
		cout <<"\n";*/
		int t=-1;
		for (int j=0;j<n;j++) if (!visit[j] && (t==-1 || abs(v[i].Snd-j)*v[i].Fst-sum[j]<abs(v[i].Snd-t)*v[i].Fst-sum[t])) t=j;
		//cout <<t<<" "<<sum[t]<<" ##\n";
		res-=sum[t];
		visit[t]=1;
	}
	cout <<res;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:36:24: error: reference to 'visit' is ambiguous
   36 |                 while (visit[l]) l++;
      |                        ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:18:6: note:                 'bool visit [2010]'
   18 | bool visit[2010];
      |      ^~~~~
a.cc:37:24: error: reference to 'visit' is ambiguous
   37 |                 while (visit[r]) r--;
      |                        ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:18:6: note:                 'bool visit [2010]'
   18 | bool visit[2010];
      |      ^~~~~
a.cc:39:24: error: reference to 'visit' is ambiguous
   39 |                 while (visit[li]) li++;
      |                        ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:18:6: note:                 'bool visit [2010]'
   18 | bool visit[2010];
      |      ^~~~~
a.cc:40:24: error: reference to 'visit' is ambiguous
   40 |                 while (visit[ri]) ri--;
      |                        ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:18:6: note:                 'bool visit [2010]'
   18 | bool visit[2010];
      |      ^~~~~
a.cc:46:44: error: reference to 'visit' is ambiguous
   46 |                 for (int j=0;j<n;j++) if (!visit[j] && (t==-1 || abs(v[i].Snd-j)*v[i].Fst-sum[j]<abs(v[i].Snd-t)*v[i].Fst-sum[t])) t=j;
      |                                            ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:18:6: note:                 'bool visit [2010]'
   18 | bool visit[2010];
      |      ^~~~~
a.cc:49:17: error: reference to 'visit' is ambiguous
   49 |                 visit[t]=1;
      |                 ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:18:6: note:                 'bool visit [2010]'
   18 | bool visit[2010];
      |      ^~~~~