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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, min, max) for (int i = (min); i < (max); ++i)
#define FORE(i, min, max) for (int i = (min); i <= (max); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPV(vec, i) for (int i = 0; i < (int)(vec.size()); ++i)
#define FOR_EACH(vec, it)                                                      \\
  for (typeof((vec).begin()) it = (vec).begin(); it != (vec).end(); ++it)

using namespace std;
static const double EPS = 1e-12;
typedef long long ll;

ll mem[2048][2048];

int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  vector<pair<int, int>> ls;
  REPV(A, i) ls.push_back(pair<int, int>{A[i], i});
  sort(ls.rbegin(), ls.rend());

  REP(i, N + 1) REP(j, N + 1) mem[i][j] = -1;
  mem[0][0] = 0;
  REP(i, N + 1) REP(j, N + 1) if (i + j < N) {
    ll &cur = mem[i][j];
    int p = i + j;
    assert(cur != -1);
    // use l
    ll next = cur + (ll)abs(i - ls[p].second) * ls[p].first;
    if (next > mem[i + 1][j]) {
      mem[i + 1][j] = next;
    }
    // use rs
    next = cur + (ll)abs((N - 1 - j) - ls[p].second) * ls[p].first;
    if (next > mem[i][j + 1]) {
      mem[i][j + 1] = next;
    }
  }

  ll ret = 0;
  REP(i, N + 1) { ret = max(ret, mem[i][N - i]); }

  cout << ret << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, min, max) for (int i = (min); i < (max); ++i)
#define FORE(i, min, max) for (int i = (min); i <= (max); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPV(vec, i) for (int i = 0; i < (int)(vec.size()); ++i)
#define FOR_EACH(vec, it)                                                      \\
  for (typeof((vec).begin()) it = (vec).begin(); it != (vec).end(); ++it)

using namespace std;
static const double EPS = 1e-12;
typedef long long ll;

ll add(ll x, ll y) {
  if (x > LONG_LONG_MAX - y)
    return LONG_LONG_MAX;
  return x + y;
}

ll Hungarian(const vector<vector<ll>> &a) {
  int n = a.size(), p, q;
  vector<ll> fx(n, LONG_LONG_MAX), fy(n, 0);
  vector<int> x(n, -1), y(n, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      fx[i] = max(fx[i], a[i][j]);
  for (int i = 0; i < n;) {
    vector<int> t(n, -1), s(n + 1, i);
    for (p = q = 0; p <= q && x[i] < 0; ++p)
      for (int k = s[p], j = 0; j < n && x[i] < 0; ++j)
        if (add(fx[k], fy[j]) == a[k][j] && t[j] < 0) {
          s[++q] = y[j], t[j] = k;
          if (s[q] < 0)
            for (p = j; p >= 0; j = p)
              y[j] = k = t[j], p = x[k], x[k] = j;
        }
    if (x[i] < 0) {
      ll d = LONG_LONG_MAX;
      for (int k = 0; k <= q; ++k)
        for (int j = 0; j < n; ++j)
          if (t[j] < 0)
            d = min(d, add(fx[s[k]], fy[j]) - a[s[k]][j]);
      for (int j = 0; j < n; ++j)
        fy[j] = add(fy[j], (t[j] < 0 ? 0 : d));
      for (int k = 0; k <= q; ++k)
        fx[s[k]] -= d;
    } else
      ++i;
  }
  ll ret = 0;
  for (int i = 0; i < n; ++i)
    ret += a[i][x[i]];
  return ret;
}

int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  vector<vector<ll>> a(N, vector<ll>(N));
  REP(i, N) REP(j, N) { a[i][j] = (ll)abs(i - j) * A[i]; }
  cout << Hungarian(a) << endl;

  return 0;
}