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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

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

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
#define INF 1LL << 60

struct infant {
  int id;
  ll score;

  bool operator<(const infant &other) const {
    if (score < other.score)
      return true;
    else
      return false;
  }
};

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  vector<infant> infants(N);
  REP(i, N) {
    infants[i].id = i;
    infants[i].score = A[i];
  }

  sort(ALL(infants));
  reverse(ALL(infants));

  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1));
  dp[0][N] = 0; // dp[l][r]: 1~l、r~Nまで入ってる

  REP(i, N) {
    infant c = infants[i];
    REP(l, i + 1) {
      int r = N + l - i;
      // if(dp[l+1][r] > dp[l][r] + c.score * abs(l - c.id)) {
      //     cerr << "l = " << l << " r = " << r << endl;
      // }
      // if(dp[l][r-1] > dp[l][r] + c.score * abs(r - 1 - c.id)) {
      //     cerr << "l = " << l << " r = " << r << " dp[l][r-1] = " <<
      //     dp[l][r-1] << endl;
      // }
      // cerr << "i = " << i << " l = " << l << " r = " << r << " dp[l][r-1] = "
      // << dp[l][r-1] << endl;
      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + c.score * abs(l - c.id));
      dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + c.score * abs(r - 1 - c.id));
    }
  }

  ll ans = 0;
  REP(i, N) if (ans < dp[i][i]) ans = dp[i][i];
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

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

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
#define INF 1LL << 60

struct infant {
  int id;
  ll score;

  bool operator<(const infant &other) const {
    if (score < other.score)
      return true;
    else
      return false;
  }
};

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];

  vector<infant> infants(N);
  REP(i, N) {
    infants[i].id = i;
    infants[i].score = A[i];
  }

  sort(ALL(infants));
  reverse(ALL(infants));

  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1));
  dp[0][N] = 0; // dp[l][r]: 1~l、r~Nまで入ってる

  REP(i, N) {
    infant c = infants[i];
    REP(l, i + 1) {
      int r = N + l - i;
      // if(dp[l+1][r] > dp[l][r] + c.score * abs(l - c.id)) {
      //     cerr << "l = " << l << " r = " << r << endl;
      // }
      // if(dp[l][r-1] > dp[l][r] + c.score * abs(r - 1 - c.id)) {
      //     cerr << "l = " << l << " r = " << r << " dp[l][r-1] = " <<
      //     dp[l][r-1] << endl;
      // }
      cerr << "i = " << i << " l = " << l << " r = " << r
           << " dp[l][r-1] = " << dp[l][r - 1] << endl;
      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + c.score * abs(l - c.id));
      dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + c.score * abs(r - 1 - c.id));
    }
  }

  ll ans = 0;
  REP(i, N) REP(j, N) if (ans < dp[i][j]) ans = dp[i][j];
  cout << ans << endl;

  return 0;
}