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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

ll dp[2005][2005];

int main() {
  int N;
  cin >> N;
  vector<pair<ll, int>> v;
  rep(i, N) {
    ll a;
    cin >> a;
    v.emplace_back(a, i);
  }
  sort(ALLOF(v));
  reverse(ALLOF(v));

  rep(i, N) {
    rep(j, i + 1) {
      int a = j;
      int b = i - j;
      dp[a + 1][b] =
          max(dp[a + 1][b], dp[a][b] + v[i].first * (v[i].second - a));
      dp[a][b + 1] = max(dp[a][b + 1],
                         dp[a][b] + v[i].first * ((N - 1 - b) - v[i].second));
    }
  }

  ll ret = 0;
  rep(i, N) { ret = max(ret, dp[i][N - i]); }
  cout << ret << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

class CostScalingMinCostFlow {
  static constexpr double alpha = 2;

public:
  struct Node {
    ll b;
    double p;
    ll in_f, out_f;
    Node() : b(0), p(0), in_f(0), out_f(0) {}
    Node(ll b, double p) : b(b), p(p), in_f(0), out_f(0) {}
  };

  struct Edge {
    ll from, to;
    ll cap, cost;
    ll rev;
    ll f;
    bool isrev;
    Edge() : from(-1), to(-1), cap(0), cost(0), rev(-1), f(0), isrev(false) {}
    Edge(ll from, ll to, ll cap, ll cost, ll rev, ll f, bool isrev)
        : from(from), to(to), cap(cap), cost(cost), rev(rev), f(f),
          isrev(isrev) {}
  };

private:
  std::vector<Node> nodes;
  std::vector<std::vector<Edge>> G;
  double epsilon;
  std::queue<ll> active_nodes;

  ll residual_cap(const Edge &e) const {
    if (!e.isrev)
      return e.cap - e.f;
    else
      return G[e.to][e.rev].f;
  }

  double reduced_cost(const Edge &e) const {
    return e.cost + nodes[e.from].p - nodes[e.to].p;
  }
  ll excess(ll i) const { return nodes[i].b - nodes[i].out_f + nodes[i].in_f; }
  bool is_active(ll i) const { return excess(i) > 0; }

  void push(Edge &edge, ll delta) {
    if (!edge.isrev) {
      edge.f += delta;
      nodes[edge.from].out_f += delta;
      nodes[edge.to].in_f += delta;
    } else {
      G[edge.to][edge.rev].f -= delta;
      nodes[edge.to].out_f -= delta;
      nodes[edge.from].in_f -= delta;
    }
  }

  void relabel(ll v) {
    double mx = std::numeric_limits<double>::lowest();
    for (Edge &edge : G[v]) {
      if (residual_cap(edge) > 0) {
        mx = std::max(mx, nodes[edge.to].p - edge.cost - epsilon);
      }
    }
    nodes[v].p = mx;
  }

  void refine() {
    for (auto &edges : G) {
      for (Edge &edge : edges) {
        if (reduced_cost(edge) >= 0)
          continue;
        if (residual_cap(edge) <= 0)
          continue;
        push(edge, residual_cap(edge));
      }
    }

    for (ll i = 0; i < nodes.size(); i++) {
      if (is_active(i))
        active_nodes.push(i);
    }

    while (!active_nodes.empty()) {
      ll v = active_nodes.front();
      active_nodes.pop();
      if (!is_active(v))
        continue;

      bool is_pushed = false;
      for (Edge &edge : G[v]) {
        if (reduced_cost(edge) >= 0)
          continue;
        if (residual_cap(edge) <= 0)
          continue;

        push(edge, std::min(residual_cap(edge), excess(v)));
        if (is_active(edge.from))
          active_nodes.push(edge.from);
        if (is_active(edge.to))
          active_nodes.push(edge.to);
        is_pushed = true;
        break;
      }

      if (!is_pushed) {
        relabel(v);
        if (is_active(v))
          active_nodes.push(v);
      }
    }
  }

public:
  CostScalingMinCostFlow(ll N) : nodes(N), G(N), epsilon(0) {}

  void add_edge(ll from, ll to, ll cap, ll cost) {
    epsilon = std::max(epsilon, (double)abs(cost));
    G[from].emplace_back(from, to, cap, cost, G[to].size(), 0, false);
    G[to].emplace_back(to, from, cap, -cost, G[from].size() - 1, cap, true);
  }

  void set_b(ll i, ll b) { nodes[i].b = b; }

  long long mincostflow() {
    ll N = nodes.size();
    while (epsilon >= 1.0 / N) {
      epsilon /= alpha;
      refine();
    }

    long long ret = 0;
    for (auto &edges : G) {
      for (Edge &edge : edges) {
        if (edge.isrev)
          continue;
        ret += edge.cost * edge.f;
      }
    }
    return ret;
  }
};

int main() {
  int N;
  cin >> N;
  vector<ll> v;
  rep(i, N) {
    ll a;
    cin >> a;
    v.push_back(a);
  }

  CostScalingMinCostFlow cs(N + N + 2);

  rep(i, N) { cs.add_edge(N + N, i, 1, 0); }
  rep(i, N) { cs.add_edge(N + i, N + N + 1, 1, 0); }

  rep(i, N) {
    rep(j, N) {
      ll s = v[i] * abs(i - j);
      cs.add_edge(i, j + N, 1, -s);
    }
  }

  cs.set_b(N + N, N);
  cs.set_b(N + N + 1, -N);

  cout << -cs.mincostflow() << endl;

  return 0;
}