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

// BUGSBUNNY
// IIT ROORKEE
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ll long long
#define double long double
#define ld long double
#define endl '\\n'
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define vl vector<long long>
#define vpii vector<pii>
#define vpipii vector<pipii>
#define vb vector<bool>
#define pb push_back
#define pob pop_back
#define mp make_pair
#define pii pair<int, int>
#define pipii pair<int, pii>
#define piipi pair<pii, int>
#define pld pair<long double, long double>
#define mod 1000000007
// #define mod 998244353
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repp(i, a, b) for (ll i = a; i < b; i++)
#define reppr(i, a, b) for (ll i = a - 1; i >= b; i--)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define ff first
#define ss second
#define pc putchar_unlocked
#define gc getchar_unlocked
#define inf 9223372036854775807
#define infn -9223372036854775807
#define pi 3.14159265358979323846
#define eps 0.0000000001
#define sp << " " <<
#define setprec(x) cout << fixed << setprecision(x);
#define REVERSE(a) reverse(all(a));
#define SORT(a) sort(all(a));
#define all(n) n.begin(), n.end()

int dp[2001][2001], n;
int vis[2001][2001];
vpii a(2001);

int hehe(int l, int r, int ind) {
  if (ind < 0)
    return 0;
  if (vis[ind][l] == 1)
    return dp[ind][l];
  vis[ind][l] = 1;
  int left = abs(a[ind].ss - l), right = abs(r - a[ind].ss);
  dp[ind][l] = max(left * a[ind].ff + hehe(l + 1, r, ind - 1),
                   right * a[ind].ff + hehe(l, r - 1, ind - 1));
  return (dp[ind][l]);
}

void solve();

signed main() {
  //-------------------------------------------------------------------------------
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  clock_t clk = clock();

  //-------------------------------------------------------------------------------

  //-------------------------------------------------------------------------------
  int t = 1, codejam = 0;
  // cin >> t;
  rep(i, t) {
    // cout << "Case #" << i+1 << ": ";
    solve();
  }
  //-------------------------------------------------------------------------------
  clk = clock() - clk;
  cerr << fixed << setprecision(12)
       << "Time: " << ((double)clk) / CLOCKS_PER_SEC;
  //-------------------------------------------------------------------------------
}

void solve() {
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i].ff, a[i].ss = i;
  SORT(a);
  cout << hehe(0, n - 1, n - 1) << endl;
}

Error: TLE

Fixed submission:

// BUGSBUNNY
// IIT ROORKEE
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ll long long
#define double long double
#define ld long double
#define endl '\\n'
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define vl vector<long long>
#define vpii vector<pii>
#define vpipii vector<pipii>
#define vb vector<bool>
#define pb push_back
#define pob pop_back
#define mp make_pair
#define pii pair<int, int>
#define pipii pair<int, pii>
#define piipi pair<pii, int>
#define pld pair<long double, long double>
#define mod 1000000007
// #define mod 998244353
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repp(i, a, b) for (ll i = a; i < b; i++)
#define reppr(i, a, b) for (ll i = a - 1; i >= b; i--)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define ff first
#define ss second
#define pc putchar_unlocked
#define gc getchar_unlocked
#define inf 9223372036854775807
#define infn -9223372036854775807
#define pi 3.14159265358979323846
#define eps 0.0000000001
#define sp << " " <<
#define setprec(x) cout << fixed << setprecision(x);
#define REVERSE(a) reverse(all(a));
#define SORT(a) sort(all(a));
#define all(n) n.begin(), n.end()

// GCD and LCM
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

// Modular Exponentiation
int powmod(int x, int y) {
  if (y == 0)
    return 1;
  int p = powmod(x, y / 2) % mod;
  p = (p * p) % mod;
  return (y % 2 == 0) ? p : (x * p) % mod;
}

// Modular Inverse
int inverse(int a) { return powmod(a, mod - 2); }

// Binomial Coefficients
int fact[200010], factinv[200010], first = 0;
void Util_nCr() {
  fact[0] = 1, factinv[0] = 1;
  repp(i, 1, 200010) {
    fact[i] = (fact[i - 1] * i) % mod;
    factinv[i] = (factinv[i - 1] * inverse(i)) % mod;
  }
}
int nCr(int n, int r) {
  if (!first)
    first = 1, Util_nCr();
  return ((fact[n] * inverse(fact[n - r]) % mod * inverse(fact[r])) % mod);
}

void solve();

signed main() {
  //-------------------------------------------------------------------------------
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  clock_t clk = clock();

  //-------------------------------------------------------------------------------

  //-------------------------------------------------------------------------------
  int t = 1, codejam = 0;
  // cin >> t;
  rep(i, t) {
    if (codejam)
      cout << "Case #" << i + 1 << ": ";
    solve();
  }
  //-------------------------------------------------------------------------------
  clk = clock() - clk;
  cerr << fixed << setprecision(12)
       << "Time: " << ((double)clk) / CLOCKS_PER_SEC;
  //-------------------------------------------------------------------------------
}

int dp[2001][2001] = {};
bool vis[2001][2001] = {};

int ans(vpii a, int l, int r, int i) {
  if (i < 0)
    return 0;
  if (vis[i][l])
    return dp[i][l];
  vis[i][l] = 1;
  int ind = a[i].ss;
  int left = abs(a[i].ss - l);
  int right = abs(a[i].ss - r);
  return dp[i][l] = max(left * a[i].ff + ans(a, l + 1, r, i - 1),
                        right * a[i].ff + ans(a, l, r - 1, i - 1));
}

void solve() {
  int n;
  cin >> n;
  vpii a(n);
  rep(i, n) cin >> a[i].ff, a[i].ss = i;
  SORT(a);
  int res = ans(a, 0, n - 1, n - 1);
  cout << res << endl;
}