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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define lb lower_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a, b)))
#define inf (long long)(1e15)
#define endl '\\n'
#define mp make_pair
#define time(s) (double(clock() - s) / double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template <typename T> void _F(const char *name, T arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename T, typename... Args>
void _F(const char *names, T arg1, Args... args) {
  const char *name = strchr(names, ',');
  cerr.write(names, name - names) << " = " << arg1 << endl;
  _F(name + 2, args...);
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &q) {
  in >> q.F >> q.S;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &q) {
  out << q.F << " " << q.S;
  return out;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F + p2.F, p1.S + p2.S};
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F - p2.F, p1.S - p2.S};
}

const int N = 2e3 + 1;
int dp[N][N], n;
vpi v;

int fun(int l, int r, int i) {
  if (l == r) {
    dp[l][r] = abs(r - v[i].S) * v[i].F;
    return dp[l][r];
  }
  if (dp[l][r] != -1)
    return dp[l][r];
  int ll = abs(r - v[i].S) * v[i].F + fun(l, r - 1, i + 1);
  int rr = abs(l - v[i].S) * v[i].F + fun(l + 1, r, i + 1);
  return dp[l][r] = max(ll, rr);
}

void solve() {
  cin >> n;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.pb({a, i});
  }
  memset(dp, -1, sizeof(dp));
  sort(rall(v));
  cout << fun(0, n - 1, 0) << endl;
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  start = clock();

  cout << fixed << setprecision(12);

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // freopen("error.txt", "w", stderr);
  // #endif

  // cin >> t;

  for (int i = 1; i <= t; ++i) {
    solve();
    // cout << endl;
  }
  // cerr << time(start);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define lb lower_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a, b)))
#define inf (long long)(1e15)
#define endl '\\n'
#define mp make_pair
#define time(s) (double(clock() - s) / double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template <typename T> void _F(const char *name, T arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename T, typename... Args>
void _F(const char *names, T arg1, Args... args) {
  const char *name = strchr(names, ',');
  cerr.write(names, name - names) << " = " << arg1 << endl;
  _F(name + 2, args...);
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &q) {
  in >> q.F >> q.S;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &q) {
  out << q.F << " " << q.S;
  return out;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F + p2.F, p1.S + p2.S};
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F - p2.F, p1.S - p2.S};
}

struct MinCostFlow {

  struct Edge {
    int f;
    int c;
    int u;
    Edge *nex;
    Edge *rev;
    Edge(int _u, int _c, int _f, Edge *_nex) : f(_f), u(_u), c(_c), nex(_nex) {}
  };
  vector<Edge *> g;

  MinCostFlow(int n) { g.resize(n); }

  void AddEdge(int v, int u, int c, int f) {
    g[v] = new Edge(u, c, f, g[v]);
    g[u] = new Edge(v, -c, 0, g[u]);
    g[v]->rev = g[u];
    g[u]->rev = g[v];
  }

  pair<int, int> Flow(int src, int sink, int n) {
    int flow = 0;
    int cost = 0;
    while (true) {
      vector<int> dis(n, inf);
      vector<Edge *> par(n);
      vector<bool> vis(n, false);
      vector<int> mn(n, 0);
      queue<int> q;

      q.push(src);
      dis[src] = 0;
      vis[src] = 1;
      mn[src] = inf;

      while (!q.empty()) {
        int v = q.front();
        q.pop();
        vis[v] = 0;
        for (Edge *e = g[v]; e; e = e->nex) {
          if (e->f <= 0)
            continue;
          if (dis[e->u] > dis[v] + e->c) {
            dis[e->u] = dis[v] + e->c;
            mn[e->u] = min(mn[v], e->f);
            par[e->u] = e;
            if (!vis[e->u]) {
              vis[e->u] = 1;
              q.push(e->u);
            }
          }
        }
      }

      if (dis[sink] == inf)
        break;
      int curr = sink;
      int ff = mn[sink];
      cost += ff * dis[sink];
      flow += ff;
      while (curr != src) {
        Edge *e = par[curr];
        e->f -= ff;
        e->rev->f += ff;
        curr = e->rev->u;
      }
    }
    return {flow, cost};
  }
} M(4010);

void solve() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    for (int j = 1; j <= n; j++) {
      M.AddEdge(i, j + n, -a[i] * abs(i - j), 1);
    }
    M.AddEdge(0, i, 0, 1);
    M.AddEdge(i + n, 2 * n + 1, 0, 1);
  }
  cout << -M.Flow(0, 2 * n + 1, 2 * n + 10).S;
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  start = clock();

  cout << fixed << setprecision(12);

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // freopen("error.txt", "w", stderr);
  // #endif

  // cin >> t;

  for (int i = 1; i <= t; ++i) {
    solve();
    // cout << endl;
  }
  // cerr << time(start);
  return 0;
}