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

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<int, int> P;
typedef tuple<int, int, int> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

using namespace std;

static const ll INF = 1e15;

const ll mod = 1000000007;

int main() {
  ll n;
  cin >> n;
  vector<P> as(n);
  rep(i, n) {
    ll a;
    cin >> a;
    as[i] = {a, i};
  }
  sort(as.begin(), as.end(), greater<P>());
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
  // 数値が大きい順に先頭か、末端に配置すればよい
  // 左からi人埋めて、右からj人埋めたときのスコア=dp[i][j]
  // dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + as[i].first  * (as[i].second - j)
  for (ll i = 0; i < n; i++) {
    for (ll left = 0; i - left >= 0; left++) {
      // dp[left][right] から dp[left+1][right], dp[left][right+1]にpush
      ll right = i - left;
      ll n1 = left + 1;
      ll add1 = as[i].first * abs(as[i].second - left);
      dp[n1][right] = max(dp[n1][right], dp[left][right] + add1);
      ll n2 = right + 1;
      ll add2 = as[i].first * abs(as[i].second - (n - 1 - right));
      dp[left][n2] = max(dp[left][n2], dp[left][right] + add2);
    }
  }
  ll u = 0;
  rep(i, n + 1) {
    rep(j, n + 1) { u = max(u, dp[i][j]); }
  }
  cout << u << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

const ll mod = 1000000007;

typedef ll cost_t;
typedef ll flow_t;

struct PrimalDual {
  const cost_t INF;

  struct edge {
    int to;
    flow_t cap;
    cost_t cost;
    int rev;
    bool isrev;
  };
  vector<vector<edge>> graph;
  vector<cost_t> potential, min_cost;
  vector<int> prevv, preve;

  PrimalDual(int V) : graph(V), INF(numeric_limits<cost_t>::max()) {}

  void add_edge(int from, int to, flow_t cap, cost_t cost) {
    graph[from].push_back({to, cap, cost, (int)graph[to].size(), false});
    graph[to].push_back({from, 0, -cost, (int)graph[from].size() - 1, true});
  }

  cost_t min_cost_flow(int s, int t, flow_t f) {
    int V = (int)graph.size();
    cost_t ret = 0;
    using Pi = pair<cost_t, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    potential.assign(V, 0);
    preve.assign(V, -1);
    prevv.assign(V, -1);

    while (f > 0) {
      min_cost.assign(V, INF);
      que.emplace(0, s);
      min_cost[s] = 0;
      while (!que.empty()) {
        Pi p = que.top();
        que.pop();
        if (min_cost[p.second] < p.first)
          continue;
        for (int i = 0; i < graph[p.second].size(); i++) {
          edge &e = graph[p.second][i];
          cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] -
                            potential[e.to];
          if (e.cap > 0 && min_cost[e.to] > nextCost) {
            min_cost[e.to] = nextCost;
            prevv[e.to] = p.second, preve[e.to] = i;
            que.emplace(min_cost[e.to], e.to);
          }
        }
      }
      if (min_cost[t] == INF)
        return -1;
      for (int v = 0; v < V; v++)
        potential[v] += min_cost[v];
      flow_t addflow = f;
      for (int v = t; v != s; v = prevv[v]) {
        addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
      }
      f -= addflow;
      ret += addflow * potential[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge &e = graph[prevv[v]][preve[v]];
        e.cap -= addflow;
        graph[v][e.rev].cap += addflow;
      }
    }
    return ret;
  }

  void output() {
    for (int i = 0; i < graph.size(); i++) {
      for (auto &e : graph[i]) {
        if (e.isrev)
          continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/"
             << rev_e.cap + e.cap << ")" << endl;
      }
    }
  }
};
int main(void) {
  ll n;
  cin >> n;
  vector<ll> as(n);
  rep(i, n) { cin >> as[i]; }
  PrimalDual g(2 * n + 2);
  rep(from, n) {
    rep(to, n) {
      ll m = abs(from - to);
      g.add_edge(from, to + n, 1, -m * as[from]);
    }
  }
  rep(to, n) { g.add_edge(2 * n, to, 1, 0); }
  rep(to, n) { g.add_edge(to + n, 2 * n + 1, 1, 0); }
  auto p = g.min_cost_flow(2 * n, 2 * n + 1, n);
  cout << -p << endl;
  return 0;
}