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

#include "bits/stdc++.h"
#define int long long
#define endl '\\n'
using namespace std;

typedef long long ll;
#define db(x) cerr << #x << ": " << x << '\\n';
#define read(a)                                                                \\
  int a;                                                                       \\
  cin >> a;
#define readb(a, b)                                                            \\
  int a, b;                                                                    \\
  cin >> a >> b;
#define readc(a, b, c)                                                         \\
  int a, b, c;                                                                 \\
  cin >> a >> b >> c;
#define readarr(a, n)                                                          \\
  int a[(n) + 1] = {};                                                         \\
  FOR(i, 1, (n)) { cin >> a[i]; }
#define readmat(a, n, m)                                                       \\
  int a[n + 1][m + 1] = {};                                                    \\
  FOR(i, 1, n) { FOR(j, 1, m) cin >> a[i][j]; }

#define print(a) cout << a << endl;
#define printarr(a, n)                                                         \\
  FOR(i, 1, n) cout << a[i] << " ";                                            \\
  cout << endl;
#define printv(v)                                                              \\
  for (int i : v)                                                              \\
    cout << i << " ";                                                          \\
  cout << endl;
#define printmat(a, n, m)                                                      \\
  FOR(i, 1, n) {                                                               \\
    FOR(j, 1, m) cout << a[i][j] << " ";                                       \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define vvi vector<vi>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  read(n);
  pi a[n + 1];
  FOR(i, 1, n) {
    read(x);
    a[i] = {x, i};
  }
  sort(a + 1, a + n + 1, greater<pi>());

  int dp[n + 1][n + 1] = {}, ans = 0;
  FOR(i, 1, n)
  FOR(j, 0, i) {
    if (j && a[i].se >= j)
      dp[i][j] = dp[i - 1][j - 1] + a[i].fi * (a[i].se - j);
    if (j < i && a[i].se <= n + 1 - i + j)
      dp[i][j] =
          max(dp[i][j], dp[i - 1][j] + a[i].fi * ((n + 1 - i + j) - a[i].se));
    if (i == n)
      ans = max(ans, dp[i][j]);
  }

  cout << ans;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define int long long
#define endl '\\n'
using namespace std;

typedef long long ll;
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}

#define read(a)                                                                \\
  int a;                                                                       \\
  cin >> a;
#define readb(a, b)                                                            \\
  int a, b;                                                                    \\
  cin >> a >> b;
#define readc(a, b, c)                                                         \\
  int a, b, c;                                                                 \\
  cin >> a >> b >> c;
#define readarr(a, n)                                                          \\
  int a[(n) + 1] = {};                                                         \\
  FOR(i, 1, (n)) { cin >> a[i]; }
#define readmat(a, n, m)                                                       \\
  int a[n + 1][m + 1] = {};                                                    \\
  FOR(i, 1, n) { FOR(j, 1, m) cin >> a[i][j]; }

#define print(a) cout << a << endl;
#define printarr(a, n)                                                         \\
  FOR(i, 1, n) cout << a[i] << " ";                                            \\
  cout << endl;
#define printv(v)                                                              \\
  for (int i : v)                                                              \\
    cout << i << " ";                                                          \\
  cout << endl;
#define printmat(a, n, m)                                                      \\
  FOR(i, 1, n) {                                                               \\
    FOR(j, 1, m) cout << a[i][j] << " ";                                       \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define vvi vector<vi>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e4 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q, sink;
struct edge {
  int x, y, cap, cost;
};

vector<edge> e;
void createEdge(int x, int y, int cap, int cost = 0) {
  e.pb({x, y, cap, cost});
  e.pb({y, x, 0, -cost});
}

void createGraph(vi &a) {
  // source to A
  FOR(i, 1, n)
  createEdge(1, i + 1, 1, 0);

  // A to B
  FOR(i, 1, n)
  FOR(j, 1, n)
  createEdge(i + 1, j + n + 1, 1, -a[i] * abs(i - j));

  // B to sink
  FOR(i, 1, n)
  createEdge(i + n + 1, sink, 1, 0);
}

void shortest_path(vi &d, vi &p) {
  d.assign(sink + 1, inf);
  p.assign(sink + 1, -1);

  d[1] = 0;
  while (true) {
    bool done = 1;
    FOR(j, 0, sz(e) - 1) {
      auto q = e[j];
      if (d[q.x] < inf && q.cap > 0 && d[q.y] > d[q.x] + q.cost) {
        d[q.y] = d[q.x] + q.cost;
        p[q.y] = j;
        done = 0;
      }
    }

    if (done)
      break;
  }
}

int max_flow() {
  vi d, p;
  int cost = 0;

  while (true) {
    shortest_path(d, p);
    if (d[sink] >= inf)
      break;

    // augment path
    int cur = sink;
    cost += d[sink];

    while (cur != 1) {
      e[p[cur]].cap--;
      e[p[cur] ^ 1].cap++;
      cur = e[p[cur]].x;
    }
  }

  return cost;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  sink = 2 * n + 2;
  vi a(n + 1);
  FOR(i, 1, n) cin >> a[i];

  createGraph(a);
  cout << -max_flow();
}