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

// #pragma GCC optimize("unroll-loops", "omit-frame-pointer", "inline")
// #pragma GCC option("arch=native", "tune=native", "no-zero-upper")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("tree-vectorize","openmp","predictive-commoning")
// #pragma GCC option("D_GLIBCXX_PARALLEL","openmp")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
// #define int long long
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define dmp(x) cerr << #x << ": " << x << endl;

template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T> vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << ',' << p.sec;
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.sec;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size())
      os << ' ';
  }
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++)
    is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

#define endl "\\n"

void solve() {
  int N;
  cin >> N;
  vector<ll> A(N);
  cin >> A;
  vector<pair<ll, int>> B;
  for (int i = 0; i < N; i++)
    B.push_back(make_pair(A[i], i));
  sort(all(B));
  reverse(all(B));
  auto dp = vect(N + 1, vect(N + 1, -LLINF));
  dp[0][0] = 0ll;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (dp[i][j] == -LLINF)
        continue;
      int l = j;
      int r = i - j;
      chmax(dp[i + 1][j + 1], dp[i][j] + B[i].first * abs(B[i].second - l));
      chmax(dp[i + 1][j],
            dp[i][j] + B[i].first * abs(B[i].second - (N - 1 - r)));
    }
  }
  cout << *max_element(all(dp[N])) << endl;
  return;
}

signed main() {
  fastio();
  solve();
  // int t; cin >> t; while(t--)solve();

  // int t; cin >> t;
  // for(int i=1;i<=t;i++){
  //   cout << "Case #" << i << ": ";
  //   solve();
  // }
  return 0;
}


Error: TLE

Fixed submission:

// #pragma GCC optimize("unroll-loops", "omit-frame-pointer", "inline")
// #pragma GCC option("arch=native", "tune=native", "no-zero-upper")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("tree-vectorize","openmp","predictive-commoning")
// #pragma GCC option("D_GLIBCXX_PARALLEL","openmp")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
#define int long long
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define dmp(x) cerr << #x << ": " << x << endl;

template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T> vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << ',' << p.sec;
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.sec;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size())
      os << ' ';
  }
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++)
    is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

#define endl "\\n"

const int MAX_V = 4010;
int V, E;
struct edge {
  int to, cap, cost, rev;
  edge(int to, int cap, int cost, int rev)
      : to(to), cap(cap), cost(cost), rev(rev) {}
};
vector<edge> G[MAX_V];
int h[MAX_V], dist[MAX_V], prevv[MAX_V], preve[MAX_V];
void add_edge(int from, int to, int cap, int cost) {
  G[from].pb(edge(to, cap, cost, G[to].size()));
  G[to].pb(edge(from, 0, -cost, G[from].size() - 1));
}
int min_cost_flow(int s, int t, int f) {
  int res = 0;
  fill(h, h + V, 0);
  while (f > 0) {
    priority_queue<P, vector<P>, greater<P>> q;
    fill(dist, dist + V, LLINF);
    dist[s] = 0;
    q.push(mp(0, s));
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.sec;
      if (dist[v] < p.fi)
        continue;
      for (int i = 0; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
          dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
          prevv[e.to] = v;
          preve[e.to] = i;
          q.push(mp(dist[e.to], e.to));
        }
      }
    }
    if (dist[t] == LLINF) {
      return -1;
    }
    for (int v = 0; v < V; v++)
      h[v] += dist[v];
    int d = f;
    for (int v = t; v != s; v = prevv[v]) {
      d = min(d, G[prevv[v]][preve[v]].cap);
    }
    f -= d;
    res += d * h[t];
    for (int v = t; v != s; v = prevv[v]) {
      edge &e = G[prevv[v]][preve[v]];
      e.cap -= d;
      G[v][e.rev].cap += d;
    }
  }
  return res;
}
// int main() {
//   // V ni kanarazu atai wo dainyuu surukoto !!!
//   // E ha fuhituyou !
//   scanf("%d", &V);
//   scanf("%d", &E);
//   for (int i = 0; i < E; i++) {
//     int a, b, cap, cost;
//     scanf("%d %d %d %d", &a, &b, &cap, &cost);
//     add_edge(a, b, cap, cost);
//   }
//   int S, T, F;
//   scanf("%d %d %d", &S, &T, &F);
//   printf("%d\\n", min_cost_flow(S, T, F));
//   return 0;
// }

void solve() {
  int N;
  cin >> N;
  V = 2 * N + 2;
  int S = 2 * N;
  int T = 2 * N + 1;
  vector<ll> A(N);
  cin >> A;
  for (int i = 0; i < N; i++) {
    add_edge(S, i, 1, 0);
    for (int j = 0; j < N; j++) {
      add_edge(i, j + N, 1, -abs(i - j) * A[i]);
    }
    add_edge(i + N, T, 1, 0);
  }
  cout << -min_cost_flow(S, T, N) << endl;
  return;
}

signed main() {
  fastio();
  solve();
  // int t; cin >> t; while(t--)solve();

  // int t; cin >> t;
  // for(int i=1;i<=t;i++){
  //   cout << "Case #" << i << ": ";
  //   solve();
  // }
  return 0;
}