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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<int, int>;
using Edge = pair<int, ll>;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<Edge>>;
const ll INF = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<pll> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i].first;
    A[i].second = i;
  }
  sort(A.begin(), A.end(), greater<pll>());

  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1));

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; i + j < N; j++) {
      dp[i + 1][j] = max(
          dp[i + 1][j], dp[i][j] + abs(A[i + j].first * (A[i + j].second - i)));
      dp[i][j + 1] =
          max(dp[i][j + 1],
              dp[i][j] + abs(A[i + j].first * (N - 1 - j - A[i + j].second)));
    }
  }
  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    ans = max(ans, dp[i][N - i]);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
// using Edge = pair<int, ll>;
// using Graph = vector<vector<int>>;
// using WeightedGraph = vector<vector<Edge>>;
const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

const int CASES = 3;

// edge class (for network-flow)
template <class FLOWTYPE, class COSTTYPE> struct Edge {
  int rev, from, to, id;
  FLOWTYPE cap, icap;
  COSTTYPE cost;
  Edge(int r, int f, int t, FLOWTYPE ca, COSTTYPE co, int id = -1)
      : rev(r), from(f), to(t), cap(ca), icap(ca), cost(co), id(id) {}
  friend ostream &operator<<(ostream &s, const Edge &E) {
    if (E.cap > 0)
      return s << E.from << "->" << E.to << '(' << E.cap << ',' << E.cost
               << ')';
    else
      return s;
  }
};

// graph class (for network-flow)
template <class FLOWTYPE, class COSTTYPE> struct Graph {
  vector<vector<Edge<FLOWTYPE, COSTTYPE>>> list;

  Graph(int n = 0) : list(n) {}
  void init(int n = 0) {
    list.clear();
    list.resize(n);
  }
  void reset() {
    for (int i = 0; i < (int)list.size(); ++i)
      for (int j = 0; j < list[i].size(); ++j)
        list[i][j].cap = list[i][j].icap;
  }
  inline vector<Edge<FLOWTYPE, COSTTYPE>> &operator[](int i) { return list[i]; }
  inline const size_t size() const { return list.size(); }

  inline Edge<FLOWTYPE, COSTTYPE> &redge(const Edge<FLOWTYPE, COSTTYPE> &e) {
    if (e.from != e.to)
      return list[e.to][e.rev];
    else
      return list[e.to][e.rev + 1];
  }

  void addedge(int from, int to, FLOWTYPE cap, COSTTYPE cost, int id = -1) {
    list[from].push_back(Edge<FLOWTYPE, COSTTYPE>((int)list[to].size(), from,
                                                  to, cap, cost, id));
    list[to].push_back(Edge<FLOWTYPE, COSTTYPE>((int)list[from].size() - 1, to,
                                                from, 0, -cost));
  }

  void add_undirected_edge(int from, int to, FLOWTYPE cap, COSTTYPE cost,
                           int id = -1) {
    list[from].push_back(Edge<FLOWTYPE, COSTTYPE>((int)list[to].size(), from,
                                                  to, cap, cost, id));
    list[to].push_back(Edge<FLOWTYPE, COSTTYPE>((int)list[from].size() - 1, to,
                                                from, cap, cost, id));
  }

  friend ostream &operator<<(ostream &s, const Graph &G) {
    s << endl;
    for (int i = 0; i < G.size(); ++i) {
      s << i << ":";
      for (auto e : G.list[i])
        s << " " << e;
      s << endl;
    }
    return s;
  }
};

// min-cost flow (by primal-dual)
template <class FLOWTYPE, class COSTTYPE>
COSTTYPE MinCostFlow(Graph<FLOWTYPE, COSTTYPE> &G, int s, int t, FLOWTYPE f) {
  int n = (int)G.size();
  vector<COSTTYPE> dist(n, -1);
  vector<int> prevv(n), preve(n), seen(n, 0);
  COSTTYPE res = 0;
  while (f > 0) {
    seen.assign(n, 0);
    dist[s] = 0;
    seen[s] = true;
    while (true) {
      bool update = false;
      for (int v = 0; v < n; ++v) {
        if (!seen[v])
          continue;
        for (int i = 0; i < G[v].size(); ++i) {
          Edge<FLOWTYPE, COSTTYPE> &e = G[v][i];
          if (e.cap > 0 && (!seen[e.to] || dist[e.to] > dist[v] + e.cost)) {
            dist[e.to] = dist[v] + e.cost;
            prevv[e.to] = v;
            preve[e.to] = i;
            seen[e.to] = true;
            update = true;
          }
        }
      }
      if (!update)
        break;
    }
    if (!seen[t])
      return -1;
    FLOWTYPE d = f;
    for (int v = t; v != s; v = prevv[v]) {
      d = min(d, G[prevv[v]][preve[v]].cap);
    }
    f -= d;
    res += dist[t] * d;
    for (int v = t; v != s; v = prevv[v]) {
      Edge<FLOWTYPE, COSTTYPE> &e = G[prevv[v]][preve[v]];
      Edge<FLOWTYPE, COSTTYPE> &re = G.redge(e);
      e.cap -= d;
      re.cap += d;
    }
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int s = 2 * n, t = 2 * n + 1;
  Graph<int, int> G(2 * n + 2);
  for (int i = 0; i < n; ++i) {
    G.addedge(s, i, 1, 0);
    G.addedge(n + i, t, 1, 0);
    for (int j = 0; j < n; ++j) {
      G.addedge(i, n + j, 1, -1 * a[i] * abs(j - i));
    }
  }
  cout << -1 * MinCostFlow(G, s, t, n) << endl;
}

int main() {
#ifdef MYLOCAL
  vector<string> filenames{"input1.txt", "input2.txt", "input3.txt",
                           "input4.txt", "input5.txt"};
  cout << "------------\\n";
  for (int i = 0; i < CASES; ++i) {
    ifstream in(filenames[i]);
    cin.rdbuf(in.rdbuf());
    solve();
    cout << "------------\\n";
  }
#else

  solve();

#endif
  return 0;
}