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

// atcoder/abc163/E/main.cpp
// author: @___Johniel
// github: https://github.com/johniel/

#include <bits/stdc++.h>

#define each(i, c) for (auto &i : c)
#define unless(cond) if (!(cond))

using namespace std;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename P, typename Q>
istream &operator>>(istream &is, pair<P, Q> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  for (auto &i : v)
    os << i << ",";
  os << ")";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, set<T> s) {
  os << "#{";
  for (auto &i : s)
    os << i << ",";
  os << "}";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> m) {
  os << "{";
  for (auto &i : m)
    os << i << ",";
  os << "}";
  return os;
}

template <typename T> inline T setmax(T &a, T b) { return a = std::max(a, b); }
template <typename T> inline T setmin(T &a, T b) { return a = std::min(a, b); }

using lli = long long int;
using ull = unsigned long long;
using point = complex<double>;
using str = string;
template <typename T> using vec = vector<T>;

constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1});
constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1});
constexpr lli mod = 1e9 + 7;

const int N = 2000 + 5;
lli memo[N][N];
vec<pair<lli, int>> v;
int n;
lli rec(int i, int j) {
  lli &ret = memo[i][j];
  if (ret != -1)
    return ret;
  unless(i <= j) return 0;

  const int k = i + (v.size() - j - 1);

  lli a = rec(i + 1, j) + v[k].first * abs(v[k].second - i);
  lli b = rec(i, j - 1) + v[k].first * abs(v[k].second - j);

  return ret = max(a, b);
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios_base::fixed);
  cout.precision(15);

  while (cin >> n) {
    vec<lli> a(n);
    cin >> a;

    v.clear();
    for (int i = 0; i < a.size(); ++i) {
      v.push_back(make_pair(a[i], i));
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());

    fill(&memo[0][0], &memo[N - 1][N - 1] + 1, -1);
    cout << rec(0, a.size() - 1) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

// atcoder/abc163/E/main.cpp
// author: @___Johniel
// github: https://github.com/johniel/

#include <bits/stdc++.h>

#define each(i, c) for (auto &i : c)
#define unless(cond) if (!(cond))

using namespace std;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename P, typename Q>
istream &operator>>(istream &is, pair<P, Q> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  for (auto &i : v)
    os << i << ",";
  os << ")";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, set<T> s) {
  os << "#{";
  for (auto &i : s)
    os << i << ",";
  os << "}";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> m) {
  os << "{";
  for (auto &i : m)
    os << i << ",";
  os << "}";
  return os;
}

template <typename T> inline T setmax(T &a, T b) { return a = std::max(a, b); }
template <typename T> inline T setmin(T &a, T b) { return a = std::min(a, b); }

using lli = long long int;
using ull = unsigned long long;
using point = complex<double>;
using str = string;
template <typename T> using vec = vector<T>;

constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1});
constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1});
constexpr lli mod = 1e9 + 7;

// https://ei1333.github.io/luzhiled/snippets/graph/primal-dual.html
template <typename flow_t, typename cost_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].emplace_back(
        (edge){to, cap, cost, (int)graph[to].size(), false});
    graph[to].emplace_back(
        (edge){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(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios_base::fixed);
  cout.precision(15);

  int n;
  while (cin >> n) {
    vec<lli> a(n);
    cin >> a;
    const int BASE = 2000 + 1;
    const int N = 4000 + 5;
    PrimalDual<int, lli> g(N);

    const int src = N - 1;
    const int snk = N - 2;

    for (int i = 0; i < n; ++i) {
      g.add_edge(src, i, 1, 0);
      g.add_edge(i + BASE, snk, 1, 0);
      for (int j = 0; j < n; ++j) {
        lli c = abs(i - j) * a[i];
        g.add_edge(i, j + BASE, 1, -c);
      }
    }
    cout << abs(g.min_cost_flow(src, snk, n)) << endl;
  }

  return 0;
}