Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef pair<ll,int> P;
int n;
vector<P> A;
ll dp[2005][2005];
ll next[2005][2005];
ll ans;
int main() {

  cin >> n;


  
  for(int i = 0; i < n; i++) {
    ll a;
    cin >> a;

    A.push_back(P(a,i));
  }

  sort(A.begin(),A.end(),greater<P> ());

  // for(int i = 0; i < n; i++) cout << A[i].first <<" "<<A[i].second<< endl;

  for(ll i = 0; i < n; i++) {
    for(ll j = 0; j <= i+1; j++) {
      ll k = i - j + 1;
      if(j == 0) {
	next[j][k] = dp[j][k-1]+abs(n-k-A[i].second)*A[i].first;
	continue;
      }
      if(k == 0) {
	next[j][k] = dp[j-1][k]+abs(A[i].second-j+1)*A[i].first;
	continue;
      }
      next[j][k] = max(dp[j-1][k]+abs(A[i].second-j+1)*A[i].first,dp[j][k-1]+abs(n-k-A[i].second)*A[i].first);
    }
    swap(next,dp);
    /* cout << i << endl;
     for(int ii = 0; ii <= i+1; ii++) {
       int j = i - ii + 1;
    cout <<"dp["<<ii<<"]["<<j<<"] = "<< dp[ii][j] << endl;
    }*/
  }
 
  for(int i = 0; i <= n; i++) {
    int j = n - i;
    ans = max(ans,dp[i][j]);
    //cout <<"dp["<<i<<"]["<<j<<"] = "<< dp[i][j] << endl;
  }
  cout << ans << endl;
    
}
  


Compiler output:
a.cc: In function 'int main()':
a.cc:34:9: error: reference to 'next' is ambiguous
   34 |         next[j][k] = dp[j][k-1]+abs(n-k-A[i].second)*A[i].first;
      |         ^~~~
In file included from /usr/include/c++/14/string:47,
                 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/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:11:4: note:                 'll next [2005][2005]'
   11 | ll next[2005][2005];
      |    ^~~~
a.cc:38:9: error: reference to 'next' is ambiguous
   38 |         next[j][k] = dp[j-1][k]+abs(A[i].second-j+1)*A[i].first;
      |         ^~~~
/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:11:4: note:                 'll next [2005][2005]'
   11 | ll next[2005][2005];
      |    ^~~~
a.cc:41:7: error: reference to 'next' is ambiguous
   41 |       next[j][k] = max(dp[j-1][k]+abs(A[i].second-j+1)*A[i].first,dp[j][k-1]+abs(n-k-A[i].second)*A[i].first);
      |       ^~~~
/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:11:4: note:                 'll next [2005][2005]'
   11 | ll next[2005][2005];
      |    ^~~~
a.cc:43:10: error: reference to 'next' is ambiguous
   43 |     swap(next,dp);
      |          ^~~~
/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:11:4: note:                 'll next [2005][2005]'
   11 | ll next[2005][2005];
      |    ^~~~