Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifdef LOCAL
#include <debug.h>
#else
#define db(...) 21
#endif
#define found(m, x) ((m).find(x) != (m).end())
#define uset unordered_set
#define umap unordered_map
#define a(x) x.begin(), x.end()
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#define input(v, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> v[i]
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pi;

const int nax = 1e5 + 7;
const int mod = 1e9 + 7; // 1e9+2667
const int inf = numeric_limits<int>::max();
// ===================== MANASH =================== //

int n, m;
int dp[2001][2001];
vector<pair<int, int>> p;
int maxdp(int s, int e) {

  int i = n - (e - s + 1);
  if (i >= n or s > e)
    return 0;
  int &ans = dp[s][e];
  if (ans != -1)
    return ans;
  int in = p[i].second;
  int left = p[i].first * (abs(s - in));
  int right = p[i].first * (abs(e - in));
  return ans = max(left + maxdp(s + 1, e), right + maxdp(s, e - 1));
}
void test() {

  cin >> n;
  p.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].first;
    p[i].second = i;
  }
  sort(a(p), greater<pair<int, int>>());
  memset(dp, -1, sizeof(dp));
  cout << maxdp(0, n - 1);
}

signed main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  cout << "Time Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec.\\n";
#endif

  int T = 1;
  // cin >> T;
  while (T--) {
    test();
    cout << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifdef LOCAL
#include <debug.h>
#else
#define db(...) 21
#endif
#define found(m, x) ((m).find(x) != (m).end())
#define uset unordered_set
#define umap unordered_map
#define a(x) x.begin(), x.end()
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#define input(v, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> v[i]
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pi;

const int nax = 1e5 + 7;
const int mod = 1e9 + 7; // 1e9+2667
const int inf = numeric_limits<int>::max();
// ===================== MANASH =================== //

int n, m;
map<tuple<int, int, int>, int> dp;
vector<pair<int, int>> p;
int maxdp(int i, int s, int e) {
  if (i == n)
    return 0;
  auto ths = make_tuple(i, s, e);
  if (dp.find(ths) != dp.end())
    return dp[ths];
  int in = p[i].second;
  int left = p[i].first * (abs(s - in));
  int right = p[i].first * (abs(e - in));
  return dp[ths] =
             max(left + maxdp(i + 1, s + 1, e), right + maxdp(i + 1, s, e - 1));
}
void test() {

  cin >> n;
  p.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].first;
    p[i].second = i;
  }
  sort(a(p), greater<pair<int, int>>());
  cout << maxdp(0, 0, n - 1);
}

signed main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  cout << "Time Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec.\\n";
#endif

  int T = 1;
  // cin >> T;
  while (T--) {
    test();
    cout << "\\n";
  }
  return 0;
}