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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()

vector<pair<ll, ll>> a;

map<pair<ll, ll>, ll> cache;

ll solve(ll left, ll right) {
  const ll cur = a.size() - (right - left);
  if (cur >= a.size()) {
    return 0;
  }

  if (cache.count({left, right})) {
    return cache[{left, right}];
  }

  ll tmp_l =
      solve(left + 1, right) + a.at(cur).first * abs(a.at(cur).second - left);
  ll tmp_r = solve(left, right - 1) +
             a.at(cur).first * abs(a.at(cur).second - (right - 1));
  const ll ret = max(tmp_l, tmp_r);
  cache[{left, right}] = ret;
  return ret;
}

int main() {
  ll n;
  cin >> n;
  REP(i, n) {
    ll ai;
    cin >> ai;
    a.emplace_back(ai, i);
  }
  sort(a.rbegin(), a.rend());
  const ll ans = solve(0, n);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()

struct MinCostFlow {
  struct Edge {
    ll to, cap, cost, rev;
  };
  using P = pair<ll, ll>;

  ll n;
  vector<vector<Edge>> g;
  vector<ll> h;
  vector<ll> dist;
  vector<ll> prevv, preve;

  MinCostFlow(ll n) : n(n), g(n), h(n), dist(n), prevv(n), preve(n) {}

  void add_edge(ll from, ll to, ll cap, ll cost) {
    g.at(from).push_back((Edge){to, cap, cost, (ll)g.at(to).size()});
    g.at(to).push_back((Edge){from, 0, -cost, (ll)g.at(from).size() - 1});
  }

  ll min_cost_flow(ll s, ll t, ll f) {
    ll res = 0;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<P, vector<P>, greater<P>> que;
      fill(dist.begin(), dist.end(), numeric_limits<ll>::max());
      dist.at(s) = 0;
      que.push(make_pair(0, s));
      while (!que.empty()) {
        const P p = que.top();
        que.pop();
        const ll v = p.second;
        if (dist.at(v) < p.first) {
          continue;
        }
        REP(i, g.at(v).size()) {
          Edge &e = g.at(v).at(i);
          if (e.cap > 0 &&
              dist.at(e.to) > dist.at(v) + e.cost + h.at(v) - h.at(e.to)) {
            dist.at(e.to) = dist.at(v) + e.cost + h.at(v) - h.at(e.to);
            prevv.at(e.to) = v;
            preve.at(e.to) = i;
            que.push(P(dist.at(e.to), e.to));
          }
        }
      }
      if (dist.at(t) == numeric_limits<ll>::max()) {
        return -1;
      }
      REP(v, n) { h.at(v) += dist.at(v); }
      ll d = f;
      for (ll v = t; v != s; v = prevv.at(v)) {
        d = min(d, g.at(prevv.at(v)).at(preve.at(v)).cap);
      }
      f -= d;
      res += d * h.at(t);
      for (ll v = t; v != s; v = prevv.at(v)) {
        Edge &e = g.at(prevv.at(v)).at(preve.at(v));
        e.cap -= d;
        g.at(v).at(e.rev).cap += d;
      }
    }
    return res;
  }
};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) { cin >> a.at(i); }

  MinCostFlow mcf(2 * (n + 1));

  // src to i
  REP(i, n) { mcf.add_edge(0, i + 1, 1, 0); }
  // j to dst
  REP(i, n) { mcf.add_edge(n + 1 + i, 2 * n + 1, 1, 0); }
  // i to j
  REP(i, n) {
    REP(j, n) {
      mcf.add_edge(i + 1, n + 1 + j, 1,
                   2000LL * 1000000000LL + 100 - a.at(i) * abs(i - j));
    }
  }

  const ll f = mcf.min_cost_flow(0, 2 * n + 1, n);

  ll ans = 0;
  REP(i, n) {
    for (const auto e : mcf.g.at(i + 1)) {
      if (e.to > i + 1 && e.cap == 0) {
        const ll j = e.to - (n + 1);
        ans += a.at(i) * abs(i - j);
      }
    }
  }
  cout << ans << endl;
  return 0;
}