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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int)v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() {
  cout << "Yes" << endl;
  exit(0);
}
inline void no() {
  cout << "No" << endl;
  exit(0);
}
template <typename T>
class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T>
class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) {
  T r = e;
  while (n > 0) {
    if (n & 1)
      r *= x;
    x *= x;
    n >>= 1;
  }
  return r;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  each(x, v) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  rep(i, v.size()) {
    if (i)
      os << ' ';
    os << v[i];
  }
  return os;
}
void solve();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(16);
  solve();
  return 0;
}

void solve() {
  int n;
  cin >> n;
  vi32 a(n);
  cin >> a;
  vi32 ord(n);
  iota(all(ord), 0);
  sort(all(ord), [&](int i, int j) { return a[i] > a[j]; });
  vector<vi64> dp(n + 1, vi64(n + 1));
  auto rec = [&](auto rec, int l, int r) -> i64 {
    if (l == r) {
      return 0;
    }
    if (dp[l][r] != 0) {
      return dp[l][r];
    }
    int i = l + (n - r);
    i64 ret = 0;
    amax(ret, rec(rec, l + 1, r) + (i64)a[ord[i]] * abs(l - ord[i]));
    amax(ret, rec(rec, l, r - 1) + (i64)a[ord[i]] * abs(r - 1 - ord[i]));
    return dp[l][r] = ret;
  };
  cout << rec(rec, 0, n) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int)v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() {
  cout << "Yes" << endl;
  exit(0);
}
inline void no() {
  cout << "No" << endl;
  exit(0);
}
template <typename T>
class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T>
class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) {
  T r = e;
  while (n > 0) {
    if (n & 1)
      r *= x;
    x *= x;
    n >>= 1;
  }
  return r;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  each(x, v) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  rep(i, v.size()) {
    if (i)
      os << ' ';
    os << v[i];
  }
  return os;
}
void solve();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(16);
  solve();
  return 0;
}

template <typename C, typename D> struct Graph {
  struct Edge {
    int to, rev;
    C cap;
    D cost;
    Edge(int to, C cap, D cost, int rev)
        : to(to), cap(cap), cost(cost), rev(rev) {}
  };
  vector<vector<Edge>> es;
  int n;
  Graph(int n) : n(n) { es.assign(n, vector<Edge>()); }
  void add_edge(int v, int u, C ca = 1, D co = 0) {
    es[v].emplace_back(u, ca, co, (int)es[u].size());
    es[u].emplace_back(v, 0, -co, (int)es[v].size() - 1);
  }
  vector<Edge> &operator[](int x) { return es[x]; }
};

template <typename C, typename D>
D min_cost_flow(Graph<C, D> &g, int s, int t, C f) {
  const D INF = numeric_limits<D>::max();
  const int n = g.n;

  D ret = 0;
  while (f > 0) {
    vector<D> dist(n, INF);
    vector<int> prevv(n), preve(n);
    dist[s] = 0;
    while (true) {
      bool update = false;
      rep(v, n) rep(i, g[v].size()) {
        auto &e = g[v][i];
        if (e.cap == 0)
          continue;
        if (dist[v] == INF || dist[e.to] <= dist[v] + e.cost)
          continue;
        dist[e.to] = dist[v] + e.cost;
        prevv[e.to] = v, preve[e.to] = i;
        update = true;
      }
      if (!update)
        break;
    }
    if (dist[t] == INF)
      return INF;
    C cap = f;
    for (int v = t; v != s; v = prevv[v]) {
      cap = min(cap, g[prevv[v]][preve[v]].cap);
    }
    f -= cap;
    ret += cap * dist[t];
    for (int v = t; v != s; v = prevv[v]) {
      auto &e = g[prevv[v]][preve[v]];
      e.cap -= cap;
      g[v][e.rev].cap += cap;
    }
  }
  return ret;
}

void solve() {
  int n;
  cin >> n;
  vi32 a(n);
  cin >> a;
  Graph<i64, i64> g(n * 2 + 2);
  reps(i, n) {
    g.add_edge(0, i, 1, 0);
    g.add_edge(n + i, n * 2 + 1, 1, 0);
    reps(j, n) { g.add_edge(i, n + j, 1, (i64)-a[i - 1] * abs(i - j)); }
  }
  auto ans = min_cost_flow<i64, i64>(g, 0, n * 2 + 1, n);
  cout << -ans << endl;
}