Problem p02709 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define eb emplace_back
#define PI 3.14159265
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.rbegin(), c.rend()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)                                           \
  (find(all(container), element) != container.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double ld;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
vector<vll> dp(3005, vll(3005, -1));
ll dfs(int nd, int l, int r, vpll &a) {
  if (l > r) {
    return 0;
  } else if (dp[l][r] != -1) {
    return dp[l][r];
  }
  return max(a[nd].F * abs(a[nd].S - l) + dfs(nd + 1, l + 1, r, a),
             a[nd].F * abs(a[nd].S - r) + dfs(nd + 1, l, r - 1, a));
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, l, r;
  cin >> n;
  vpll a(n);
  fr(i, 0, n) {
    cin >> a[i].F;
    a[i].S = i;
  }
  sort(all(a));
  ll ans = dfs(0, 0, n - 1, a);
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define eb emplace_back
#define PI 3.14159265
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.rbegin(), c.rend()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)                                           \
  (find(all(container), element) != container.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double ld;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
vector<vll> dp(3005, vll(3005, -1));
ll dfs(int nd, int l, int r, vpll &a) {
  if (l > r) {
    return 0;
  } else if (dp[l][r] != -1) {
    return dp[l][r];
  }
  return dp[l][r] = max(a[nd].F * abs(a[nd].S - l) + dfs(nd + 1, l + 1, r, a),
                        a[nd].F * abs(a[nd].S - r) + dfs(nd + 1, l, r - 1, a));
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, l, r;
  cin >> n;
  vpll a(n);
  fr(i, 0, n) {
    cin >> a[i].F;
    a[i].S = i;
  }
  sort(all(a));
  ll ans = dfs(0, 0, n - 1, a);
  cout << ans << "\n";
  return 0;
}