Language: C++

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok?"Yes":"No");
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef map<string, int> msi;
typedef greater<int> gt;
typedef priority_queue<int, vector<int>, gt> minq;
typedef long long ll;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
//clang++ -std=c++11 -stdlib=libc++ 

int main() {
  ll N; cin>>N;
  vector<ll> A(N);
  rep(i,N) cin >> A[i];
  vector<vector<ll>> ord(N);
  rep(i,N) ord[i] = pair<ll,ll>(i, A[i]);
  sort(ord.begin(), ord.end(), [](pair<ll,ll> a, pair<ll,ll> b) {return a.second>b.second;});
  vector<vector<ll>> dp(N+1, vector<ll>(N+1,0)); // total when there's l babies on the left and r on the right

  ll ans = 0;
  rep(l, N) {
    rep(r,N) {
      if (l+r==N) {
        ans = max(ans, dp[l][r]);
        break;
      }
      ll idx = l+r;
      dp[l+1][r] = max(dp[l+1][r], dp[l][r]+ord[idx].second*abs(ord[idx].first-l));
      dp[l][1+r] = max(dp[l][r+1], dp[l][r]+ord[idx].second*abs((N-1-r) - ord[idx].first));
    }
  }
 printf("%lld\n", ans);
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:26:40: error: no match for 'operator=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::vector<long long int> >, std::vector<long long int> >::value_type' {aka 'std::vector<long long int>'} and 'std::pair<long long int, long long int>')
   26 |   rep(i,N) ord[i] = pair<ll,ll>(i, A[i]);
      |                                        ^
In file included from /usr/include/c++/14/vector:72,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/vector.tcc:210:5: note: candidate: 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = long long int; _Alloc = std::allocator<long long int>]'
  210 |     vector<_Tp, _Alloc>::
      |     ^~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:211:42: note:   no known conversion for argument 1 from 'std::pair<long long int, long long int>' to 'const std::vector<long long int>&'
  211 |     operator=(const vector<_Tp, _Alloc>& __x)
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
In file included from /usr/include/c++/14/vector:66:
/usr/include/c++/14/bits/stl_vector.h:766:7: note: candidate: 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = long long int; _Alloc = std::allocator<long long int>]'
  766 |       operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:766:26: note:   no known conversion for argument 1 from 'std::pair<long long int, long long int>' to 'std::vector<long long int>&&'
  766 |       operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
      |                 ~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:788:7: note: candidate: 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_Tp>) [with _Tp = long long int; _Alloc = std::allocator<long long int>]'
  788 |       operator=(initializer_list<value_type> __l)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:788:46: note:   no known conversion for argument 1 from 'std::pair<long long int, long long int>' to 'std::initializer_list<long long int>'
  788 |       operator=(initializer_list<value_type> __l)
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
a.cc:38:54: error: '__gnu_cxx::__alloc_traits<std::allocator<std::vector<long long int> >, std::vector<long long int> >::value_type' {aka 'class std::vector<long long int>'} has no member named 'second'
   38 |       dp[l+1][r] = max(dp[l+1][r], dp[l][r]+ord[idx].second*abs(ord[idx].first-l));
      |                                                      ^~~~~~
a.cc:38:74: error: '__gnu_cxx::__alloc_traits<std::allocator<std::vector<long long int> >, std::vector<long long int> >::value_type' {aka 'class std::vector<long long int>'} has no member named 'first'
   38 |       dp[l+1][r] = max(dp[l+1][r], dp[l][r]+ord[idx].second*abs(ord[idx].first-l));
      |                                                                          ^~~~~
a.cc:39:54: error: '__gnu_cxx::__alloc_traits<std::allocator<std::vector<long long int> >, std::vector<long long int> >::value_type' {aka 'class std::vector<long long int>'} has no member named 'second'
   39 |       dp[l][1+r] = max(dp[l][r+1], dp[l][r]+ord[idx].second*abs((N-1-r) - ord[idx].first));
      |                                                      ^~~~~~
a.cc:39:84: error: '__gnu_cxx::__alloc_traits<std::allocator<std::vector<long long int> >, std::vector<long long int> >::value_type' {aka 'class std::vector<long long int>'} has no member named 'first'
   39 |       dp[l][1+r] = max(dp[l][r+1], dp[l][r]+ord[idx].second*abs((N-1-r) - ord[idx].first));
      |                                                                                    ^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<std::vector<long long int>*, std::vector<std::vector<long long int> > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::vector<long long int>*, std::vector<std::vector<long long int> > >; _Compare = main()::<lambda(std::pair<long long int, long long int>, std::pair<long long int, long long int>)>]':
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<vector<long long int>*, vector<vector<long long int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(pair<long long int, long long int>, pair<long long int, long long int>)> >]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<vector<long long int>*, vector<vector<long long int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(pair<long long int, long long int>, pair<long long int, long long int>)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<vector<long long int>*, vector<vector<long long int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(pair<long long int, long long int>, pair<long long int, long long int>)> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<vector<long long int>*, vector<vector<long long int> > >; _Compare = main()::<lambda(pair<long long int, long long int>, pair<long long int, long long int>)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:27:7:   required from here
   27 |   sort(ord.begin(), ord.end(), [](pair<ll,ll> a, pair<ll,ll> b) {return a.second>b.second;});
      |   ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:30: error: no match for call to '(main()::<lambda(std::pair<long long int, long long int>, std::pair<long long int, long long int>)>) (std::vector<long long int>&, std::vector<long long int>&)'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:30: note: candidate: 'bool (*)(std::pair<long long int, long long int>, std::pair<long long int, long long int>)' (conversion)
/usr/include/c++/14/bits/predefined_ops.h:158:30: note:   candidate expects 3 arguments, 3 provided
a.cc:27:32: note: candidate: 'main()::<lambda(std::pair<long long int, long long int>, std::pair<long long int, long long int>)>'
   27 |   sort(ord.begin(), ord.end(), [](pair<ll,ll> a, pair<ll,ll> b) {return a.second>b.second;});
      |                                ^
a.cc:27:32: note:   no known conversion for argument 1 from 'std::vector<long long int>' to 'std::pair<long long int, long long int>'
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = std::vector<long long int>; _Iterator = __gnu_cxx::__normal_iterator<std::vector<long long int>*, std::vector<std::vector<long long int> > >; _Compare = main()::<lambda(std::pair<long long int, long long int>, std::pair<long long int, long long int>)>]':
/usr/include/c++/14/bits/stl_algo.h:1757:20:   required from 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<vector<long long int>*, vector<vector<long long int> > >; _Compare = __gnu_cxx::__ops::_Val_comp_iter<main()::<lambda(pair<long long int, long long int>, pair<long long int, long long int>)> >]'
 1757 |       while (__comp(__val, __next))
      |              ~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1785:36:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<vector<long long int>*, vector<vector<long long int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(pair<long long int, long long int>, pair<long long int, long long int>)> >]'
 1785 |             std::__unguarded_linear_insert(__i,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
 1786 |                                 __gnu_cxx::__ops::__val_comp_iter(__comp));
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<vector<long long int>*, vector<vector<long long int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(pair<long long int, long long int>, pair<long long int, long long int>)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomA