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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template <typename T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  vector<LP> as(n);
  rep(i, n) {
    ll a;
    cin >> a;
    as[i].fi = a;
    as[i].se = i;
  }
  sort(rng(as));
  reverse(rng(as));
  vvl dp(n, vl(n, -INF));
  dp[0][n - 1] = 0;
  rep(i, n) rep(j, i + 1) {
    ll l = j, r = n - i + j - 1;
    if (l + 1 < n)
      chmax(dp[l + 1][r], dp[l][r] + as[i].fi * abs(as[i].se - l));
    if (r - 1 >= 0)
      chmax(dp[l][r - 1], dp[l][r] + as[i].fi * abs(as[i].se - r));
  }
  ll ans = 0;
  rep(i, n) rep(j, n) chmax(ans, dp[i][j]);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template <typename T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  vector<LP> as(n);
  rep(i, n) {
    ll a;
    cin >> a;
    as[i].fi = a;
    as[i].se = i;
  }
  sort(rng(as));
  reverse(rng(as));
  vector<vvl> dp(n + 1, vvl(n, vl(n, -INF)));
  dp[0][0][n - 1] = 0;
  rep(i, n) rep(j, i + 1) {
    ll l = j, r = n - i + j - 1;
    if (l + 1 < n)
      chmax(dp[i + 1][l + 1][r], dp[i][l][r] + as[i].fi * abs(as[i].se - l));
    if (r - 1 >= 0)
      chmax(dp[i + 1][l][r - 1], dp[i][l][r] + as[i].fi * abs(as[i].se - r));
  }
  ll ans = 0;
  rep(i, n) rep(j, n) chmax(ans, dp[n][i][j]);
  cout << ans << endl;
}