alexjercan/bugnet huggingface dataset:
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
using namespace std;

int main() {
  pair<long long, int> order[2020];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long temp;
    cin >> temp;
    order[i] = make_pair(temp, i + 1);
  }
  sort(order, order + n);
  reverse(order, order + n);
  long long dp[2020][2020];
  for (int x = 0; x <= n; x++) {
    for (int y = 0; y <= n; y++) {
      dp[x][y] = -1;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    long long k = order[i].first;
    int bef = order[i].second;
    for (int x = 0; x <= i; x++) {
      int y = i - x;
      dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + abs(bef - (x + 1)) * k);
      dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + abs(bef - (n - y)) * k);
    }
  }
  long long sum = 0;
  for (int i = 0; i <= n; i++) {
    sum = max(sum, dp[i][n - i]);
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
using namespace std;

int main() {
  pair<long long, int> order[2020];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long temp;
    cin >> temp;
    order[i] = make_pair(temp, i + 1);
  }
  sort(order, order + n);
  reverse(order, order + n);
  long long dp[1000][1000];
  for (int x = 0; x <= n; x++) {
    for (int y = 0; y <= n; y++) {
      dp[x][y] = -1;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    long long k = order[i].first;
    int bef = order[i].second;
    for (int x = 0; x <= i; x++) {
      int y = i - x;
      dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + abs(bef - (x + 1)) * k);
      dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + abs(bef - (n - y)) * k);
    }
  }
  long long sum = 0;
  for (int i = 0; i <= n; i++) {
    sum = max(sum, dp[i][n - i]);
  }
  cout << sum << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <deque>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

typedef long long ll;

deque<pair<ll, ll>> v;
ll dp[2222][2222], n;

int main(void) {
  ll a, i, j;

  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a);
    v.push_back(make_pair(a, i));
  }

  sort(v.begin(), v.end(), greater<>());

  for (i = 1; i <= n; i++)
    for (j = i; j >= 1; j--) {
      ll cnt = j - 1 + n - i;

      if (i == j)
        dp[j][i] = v[cnt].first * abs(j - v[cnt].second);
      dp[j][i] = max(dp[j + 1][i] + v[cnt].first * abs(v[cnt].second - j),
                     dp[j][i - 1] + v[cnt].first * abs(v[cnt].second - i));
    }

  printf("%lld", dp[1][n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

typedef long long ll;

deque<pair<ll, ll>> v;
deque<ll> L;
ll dp[2222][2222], n;

ll solve(ll s, ll e, ll val) {
  ll cnt = s - 1 + n - e;

  if (s == e)
    return val + v[cnt].first * abs(s - v[cnt].second);
  else
    return dp[s][e] = max(
               dp[s][e],
               max(solve(s + 1, e, val + v[cnt].first * abs(v[cnt].second - s)),
                   solve(s, e - 1,
                         val + v[cnt].first * abs(v[cnt].second - e))));
}

int main(void) {
  ll a, i;

  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a);
    v.push_back(make_pair(a, i));
  }

  sort(v.begin(), v.end(), greater<>());

  printf("%lld", solve(1, n, 0));
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x7fffffff
#define INFL 0x7fffffffffffffffLL

vector<pair<ll, ll>> a(2000);
ll n;
ll dp[2000][2000];

ll calc(ll x, ll y) {
  int idx = x + ((n - 1) - y);
  if (idx == n)
    return 0;
  if (dp[x][y] != 0)
    return dp[x][y];
  ll ans = max(abs(a[idx].second - x) * a[idx].first + calc(x + 1, y),
               abs(a[idx].second - y) * a[idx].first + calc(x, y - 1));
  dp[x][y] = ans;
  return ans;
}

int main() {
  ll b, c, i, j, k, m, x, y, ans = 0;
  string str;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> j;
    a[i] = make_pair(j, i);
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());

  x = 0;
  y = n - 1;

  ans = calc(x, y);

  cout << ans << endl;
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x7fffffff
#define INFL 0x7fffffffffffffffLL

vector<pair<ll, ll>> a(2000);
ll n;
ll dp[2000][2000];

ll calc(ll x, ll y) {
  int idx = x + ((n - 1) - y);
  if (idx == n)
    return 0;
  // if (dp[x][y]!=0) return dp[x][y];
  ll ans = max(abs(a[idx].second - x) * a[idx].first + calc(x + 1, y),
               abs(a[idx].second - y) * a[idx].first + calc(x, y - 1));
  dp[x][y] = ans;
  return ans;
}

int main() {
  ll b, c, i, j, k, m, x, y, ans = 0;
  string str;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> j;
    a[i] = make_pair(j, i);
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());

  x = 0;
  y = n - 1;

  ans = calc(x, y);

  cout << ans << endl;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)
#define MOD 1'000'000'007

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

long long dp[2222][2222];

int main() {
  int N;
  cin >> N;
  vector<pair<long long, long long>> A(N);
  REP(i, N) {
    long long a;
    cin >> a;
    A[i] = make_pair(a, i);
  }
  sort(A.begin(), A.end(), greater<pair<long long, long long>>());

  vector<vector<long long>> dp(2222, vector<long long>(2222, -(1LL << 60)));
  dp[0][N] = 0;

  for (int i = 0; i < N; i++) {
    long long x = A[i].first;
    long long pos = A[i].second;
    for (int l = 0; l <= i; l++) {
      int r = l + N - i;
      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + x * abs(pos - l));
      dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + x * abs(pos - (r - 1)));
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dp[i][i]);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)
#define MOD 1'000'000'007

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

long long dp[2222][2222];

int main() {
  int N;
  cin >> N;
  vector<pair<long long, long long>> A(N);
  REP(i, N) {
    long long a;
    cin >> a;
    A[i] = make_pair(a, i);
  }
  sort(A.begin(), A.end(), greater<pair<long long, long long>>());

  vector<vector<long long>> dp(2222, vector<long long>(2222, -(1LL << 60)));
  dp[0][N] = 0;

  for (int i = 0; i < N; i++) {
    long long x = A[i].first;
    long long pos = A[i].second;
    for (int l = 0; l < N; l++) {
      int r = l + N - i;
      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + x * abs(pos - l));
      dp[l][r - 1] = max(dp[l][r - 1], dp[l][r] + x * abs(pos - (r - 1)));
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dp[i][i]);
  cout << ans << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
const int mod = 1e9 + 7;
typedef long long ll;
const int N = 2e3 + 5;

int n;
std::vector<std::pair<ll, int>> v;
ll dp[N][N];
ll solve(int l, int r) {
  if (dp[l][r] >= 0)
    return dp[l][r];
  if (r < l)
    return dp[l][r] = 0;
  int i = n - r + l; // 当前正在放第i个
  return dp[l][r] =
             std::max(v[i].first * std::abs(v[i].second - l) + solve(l + 1, r),
                      v[i].first * std::abs(v[i].second - r) + solve(l, r - 1));
}
int main() {
  // freopen("a.in","r",stdin);
  scanf("%d", &n);
  v.push_back(std::make_pair(0, 0));
  for (int i = 1; i <= n; i++) {
    ll j;
    scanf("%lld", &j);
    v.push_back(std::make_pair(j, i));
  }
  std::sort(v.rbegin(), v.rend() - 1);
  memset(dp, -1, sizeof(dp));
  printf("%lld\\n", solve(1, n));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const int mod = 1e9 + 7;
typedef long long ll;
const int N = 2e3 + 5;

int n;
std::vector<std::pair<ll, int>> v;
ll dp[N][N];
ll solve(int l, int r) {
  if (dp[l][r] >= 0)
    return dp[l][r];
  if (r < l)
    return dp[l][r] = 0;
  int i = n - r + l; // 当前正在放第i个
  return std::max(v[i].first * std::abs(v[i].second - l) + solve(l + 1, r),
                  v[i].first * std::abs(v[i].second - r) + solve(l, r - 1));
}
int main() {
  // freopen("a.in","r",stdin);
  scanf("%d", &n);
  v.push_back(std::make_pair(0, 0));
  for (int i = 1; i <= n; i++) {
    ll j;
    scanf("%lld", &j);
    v.push_back(std::make_pair(j, i));
  }
  std::sort(v.rbegin(), v.rend() - 1);
  memset(dp, -1, sizeof(dp));
  printf("%lld\\n", solve(1, n));
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define mes(a, b) memset(a, b, sizeof a)
#define pb push_back
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 2e6 + 10;
const double eps = 1e-1;
ll n;
ll l, r;
ll max(ll x, ll y) { return x > y ? x : y; }
struct Node {
  ll x, id;
  bool operator<(const Node &p) const { return x > p.x; }
} a[maxn];
ll dp[2100][2100];
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i].x);
    a[i].id = i;
  }
  sort(a + 1, a + n + 1);
  ll tmp = 0, ans = 0;
  Node b;
  for (int i = 1; i <= n; i++) {
    b = a[i];
    for (ll l = 1, r = n - i + 1; r <= n; l++, r++) {
      dp[i][l] = max(dp[i][l], dp[i - 1][l - 1] + abs(b.id - l) * b.x);
      dp[i][l - 1] = max(dp[i][l - 1], dp[i - 1][l - 1] + abs(b.id - r) * b.x);
    }
  }
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[n][i]);
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define mes(a, b) memset(a, b, sizeof a)
#define pb push_back
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 2e6 + 10;
const double eps = 1e-1;
ll n;
ll l, r;
ll max(ll x, ll y) { return x > y ? x : y; }
struct Node {
  ll x, id;
  bool operator<(const Node &p) const { return x > p.x; }
} a[maxn];
ll dp[210][2100];
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i].x);
    a[i].id = i;
  }
  sort(a + 1, a + n + 1);
  ll tmp = 0, ans = 0;
  Node b;
  for (int i = 1; i <= n; i++) {
    b = a[i];
    for (ll l = 1, r = n - i + 1; r <= n; l++, r++) {
      dp[i][l] = max(dp[i][l], dp[i - 1][l - 1] + abs(b.id - l) * b.x);
      dp[i][l - 1] = max(dp[i][l - 1], dp[i - 1][l - 1] + abs(b.id - r) * b.x);
    }
  }
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[n][i]);
  printf("%lld\\n", ans);
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back

#define ll long long

#define int ll

#define mp make_pair

using namespace std;

const int inf = 1e9 + 7;

const ll ll_inf = 1e18 + 7;
const int K = 2e5;
const int N = 2e5 + 300;

ll binpow(ll a, ll n) {
  if (n == 0)
    return 1ll;
  ll res = 1;
  while (n) {
    if (n & 1) {
      res *= a;
      res %= inf;
    }
    a *= a;
    n >>= 1;
    a %= inf;
  }
  return res;
}

void boost() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int n, dp[2010][2010];

pair<int, int> a[2020];

int rec(int l, int r, int v) {
  if (l == r)
    return 0;

  if (dp[l][r] != -1)
    return dp[l][r];
  dp[l][r] = max(rec(l + 1, r, v - 1) + abs(l - a[v].s) * a[v].f,
                 rec(l, r - 1, v - 1) + abs(r - 1 - a[v].s) * a[v].f);
  return dp[l][r];
}

main() {
  cin >> n;
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= n + 1; ++j)
      dp[i][j] = -1;
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].f;
    a[i].s = i;
  }
  sort(a + 1, a + n + 1);
  cout << rec(1, n + 1, n);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int cal(int a[], int n) {
  int sum = 0, p, q;
  n = n / 2;
  for (int i = 0; i < n; i++) {
    int temp;
    p = a[i] * (abs((i + 1) - (n + i + 1)));
    q = a[n + i] * (abs((n + i + 1) - (i + 1)));
    temp = a[i];
    a[i] = a[n + i];
    a[n + i] = temp;

    sum = sum + p + q;
  }
  cout << sum;
}

int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cal(a, n);

  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
using Graph = vector<vector<ll>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
/****** define constant end ********/
/****** define variable start ******/
ll n;
vpll a;
/******* define variable end *******/
int solve() {
  cin >> n;
  a.resize(n);
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    a[i] = make_pair(tmp, i + 1);
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  vll dp(n + 1, vl(n + 1));
  REP(i, n + 1) {
    REP(j, n + 1) { dp[i][j] = -INF; }
  }

  dp[0][0] = 0;
  REP(i, n) {
    REP(j, n) {
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1],
                             dp[i][j] + a[i].first * abs(n - j - a[i].second));
      dp[i + 1][j] = max(
          dp[i + 1][j], dp[i][j] + a[i].first * abs(1 + (i - j) - a[i].second));
    }
  }
  ll ans = 0;
  REP(i, n + 1) { ans = max(ans, dp[n][i]); }
  cout << ans << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> pair_t;
using Graph = vector<vector<ll>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
/****** define constant end ********/
/****** define variable start ******/
ll n;
vl a;
/******* define variable end *******/
int solve() {
  cin >> n;
  a.resize(n);
  vl nums(n);
  REP(i, n) {
    cin >> a[i];
    nums[i] = i;
  }
  vector<vector<ll>> cost(n, vector<ll>(n));
  REP(i, n) {
    REP(j, n) { cost[i][j] = abs(i - j) * a[i]; }
  }
  ll ans = 0;
  do {
    ll anstmp = 0;
    REP(i, n) { anstmp += cost[i][nums[i]]; }
    if (anstmp > ans) {
      ans = anstmp;
    }
  } while (next_permutation(nums.begin(), nums.end()));
  cout << ans << endl;
  return 0;
}

int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  rep(i, n) {
    cin >> a[i].first;
    a[i].second = i + 1;
  }
  // dp[x][y] =　A が大きい方から (x + y) 人選び，うち x 人を n, n - 1, ...
  // の位置に動かした時の最大値 dp[x+1][y] = dp[x][y] + a[x+y].first * (x -
  // a[x+y].second) dp[x][y+1] = dp[x][y] + a[x+y].first * (a[x+y].second - y)
  sort(a.begin(), a.end(), greater<pair<ll, ll>>());
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -1));
  dp[0][0] = 0;
  for (ll s = 1; s <= n; s++) {
    for (ll x = 0; x <= s; x++) {
      if (s - x - 1 >= 0)
        chmax(dp[x][s - x],
              dp[x][s - x - 1] + a[s - 1].first * (a[s - 1].second - (s - x)));
      if (x - 1 >= 0)
        chmax(dp[x][s - x],
              dp[x - 1][s - x] +
                  a[s - 1].first * ((n - x + 1) - a[s - 1].second));
    }
  }
  ll res = -1;
  for (ll i = 0; i <= n; i++) {
    chmax(res, dp[i][n - i]);
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll res = 0;
  vector<bool> used(n, false);
  for (ll i = 0; i < (n + 1) / 2; i++) {
    ll tmp = 0, who = -1;
    for (ll j = i + 1; j < n; j++) {
      if (used[j])
        continue;
      if (tmp <= abs(j - i) * a[j]) {
        tmp = abs(j - i) * a[j];
        who = j;
      }
    }
    used[who] = true;
    res += tmp;
    tmp = 0;
    if (n % 2 == 1 && i == (n - 1) / 2)
      continue;
    for (ll j = n - i - 2; j >= 0; j--) {
      if (used[j])
        continue;
      if (tmp <= abs(j - (n - 1 - i)) * a[j]) {
        tmp = abs(j - (n - 1 - i)) * a[j];
        who = j;
      }
    }
    used[who] = true;
    res += tmp;
  }
  cout << res << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

using P = pair<int, int>;

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  vector<P> p(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    p[i] = {a[i], i};
  }
  sort(p.rbegin(), p.rend());

  auto dp = make_vec<int>(n + 1, n + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; i + j < n; j++) {
      dp[i + 1][j] =
          max(dp[i + 1][j], dp[i][j] + p[i + j].first * (p[i + j].second - i));
      dp[i][j + 1] =
          max(dp[i][j + 1],
              dp[i][j] + p[i + j].first * ((n - 1 - j) - p[i + j].second));
    }
  }

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i + j == n)
        ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

using P = pair<int, int>;

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  vector<P> p(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    p[i] = {a[i], i};
  }
  sort(p.rbegin(), p.rend());

  auto dp = make_vec<int>(n + 1, n + 1);
  auto dfs = [&](auto &&dfs, int l, int r) -> void {
    if (l + r == n)
      return;
    int i = l + r;
    dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + p[i].first * (p[i].second - l));
    dp[l][r + 1] =
        max(dp[l][r + 1], dp[l][r] + p[i].first * ((n - 1 - r) - p[i].second));
    dfs(dfs, l + 1, r);
    dfs(dfs, l, r + 1);
  };
  dfs(dfs, 0, 0);

  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i + j == n)
        ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << endl;

  return 0;
}