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

/* ****TCIITB**** */

#include <bits/stdc++.h>
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll int long long
#define pb push_back
#define endl '\\n'
#define pii pair<int long long, int long long>
#define vi vector<int long long>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int long long)x.size()
#define hell 1000000007
#define rep(i, a, b) for (int long long i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;

#define N 2007
ll n;
std::vector<pii> a(N);

ll dp[N][N];
ll func(ll pos, ll st, ll en) {
  if (pos == (n))
    return 0;
  if (dp[pos][st] != -1)
    return dp[pos][st];
  ll res = (abs(a[pos].S - st) * a[pos].F) + func(pos + 1, st + 1, en);
  res = max(res, (abs(a[pos].S - en) * a[pos].F) + func(pos + 1, st, en - 1));
  return dp[pos][st] = max(dp[pos][st], res);
}
int main() {

  io

          cin >>
      n;
  memset(dp, -1, sizeof(dp));
  rep(i, 0, n) {
    ll x;
    cin >> x;
    a[i] = {x, i};
  }
  sort(all(a));
  reverse(all(a));
  cout << func(0, 0, n - 1);
}

Error: TLE

Fixed submission:

/* ****TCIITB**** */

#include <bits/stdc++.h>
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define pb push_back
#define endl '\\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;

#define N 2007
ll n;
std::vector<ll> a(N);
struct Comparer {
  bool operator()(const bitset<N> &b1, const bitset<N> &b2) const {
    return b1.to_ulong() < b2.to_ulong();
  }
};
std::unordered_map<bitset<N>, ll> dp[N];
ll res = 0;
ll func(ll pos, bitset<N> taken) {
  if (pos == (n + 1)) {
    return 0;
  }
  if (dp[pos].find(taken) != dp[pos].end())
    return dp[pos][taken];
  ll cres = 0;
  rep(i, 1, n + 1) {
    if (!taken[i]) {
      taken[i] = 1;
      ll ex = abs(pos - i) * a[pos];
      // cout<<pos<<" "<<i<<" "<<ex<<endl;
      cres = max(cres, ex + func(pos + 1, taken));
      taken[i] = 0;
    }
  }
  return dp[pos][taken] = max(dp[pos][taken], cres);
}
int main() {

  io

          cin >>
      n;
  rep(i, 0, n) cin >> a[i + 1];
  bitset<N> pos;
  cout << func(1, pos);
}