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

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

// g++ -g -o yourexe yourfile.cpp
//./yourexe < yourinput.in > youroutput.out

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define M 1000000007

#define ll long long
#define ld long double
#define vi vector<ll>
#define pi pair<ll, ll>
#define vii vector<pi>
#define vvi vector<vi>
#define pb push_back
#define endl "\\n"

#define REP(i, s, e) for (ll i = s; i < e; i++)
#define RREP(i, s, e) for (ll i = s; i > e; i--)

#define all(v) v.begin(), v.end()
#define part(v, s, e) v.begin() + s, v.begin() + e
#define print(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";

int main() {
  fast;
  ll n;
  cin >> n;
  vii arr(n);
  REP(i, 0, n) {
    cin >> arr[i].first;
    arr[i].second = i;
  }
  vvi dp(n, vi(n + 1, -1));
  sort(all(arr), greater<pi>());
  dp[0][1] = abs(arr[0].second - 0) * arr[0].first;
  dp[0][0] = abs(arr[0].second - (n - 1)) * arr[0].first;
  REP(i, 1, n) {
    REP(j, 0, i + 1) {
      dp[i][j] = max(dp[i][j],
                     dp[i - 1][j] +
                         arr[i].first * abs(arr[i].second - (n - 1 - (i - j))));
      dp[i][j + 1] = max(dp[i][j + 1],
                         dp[i - 1][j] + arr[i].first * abs(arr[i].second - j));
    }
  }
  cout << *max_element(all(dp[n - 1])) << endl;
}


Error: 0

Fixed submission:

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

// g++ -g -o yourexe yourfile.cpp
//./yourexe < yourinput.in > youroutput.out

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define M 1000000007

#define ll long long
#define ld long double
#define vi vector<ll>
#define pi pair<ll, ll>
#define vii vector<pi>
#define vvi vector<vi>
#define pb push_back
#define endl "\\n"

#define REP(i, s, e) for (ll i = s; i < e; i++)
#define RREP(i, s, e) for (ll i = s; i > e; i--)

#define all(v) v.begin(), v.end()
#define part(v, s, e) v.begin() + s, v.begin() + e
#define print(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";

struct ind {
  ll v, i, j;

  bool operator<(ind &a) { return this->v >= a.v; }
};

int main() {
  fast;
  ll n;
  cin >> n;
  vi arr(n);
  REP(i, 0, n)
  cin >> arr[i];
  vi vis1(n, 0), vis2(n, 0);
  vector<ind> tmp;
  REP(i, 0, n)
  REP(j, 0, n)
  tmp.pb({arr[i] * abs(j - i), i, j});
  sort(all(tmp));
  ll sum = 0;
  REP(i, 0, tmp.size()) {
    if (!vis1[tmp[i].i] && !vis2[tmp[i].j]) {
      sum += tmp[i].v;
      vis1[tmp[i].i] = 1;
      vis2[tmp[i].j] = 1;
    }
  }
  cout << sum << endl;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cfenv>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <forward_list>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define MOD 1000000009
#define SIZE 2002
typedef long long ll;
using namespace std;

int main() {
  ll N;
  vector<pair<ll, ll>> A;
  cin >> N;
  for (ll i = 0; i < N; i++) {
    ll tmp;
    cin >> tmp;
    A.push_back({tmp, i});
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  ll DP[SIZE][SIZE];
  for (int i = 0; i < SIZE; i++) {
    for (int j = 0; j < SIZE; j++) {
      DP[i][j] = 0;
    }
  }
  for (int i = 0; i < N; i++) {
    ll act = A[i].first;
    ll no = A[i].second;
    for (int x = 0; x <= i; x++) {
      int y = i - x;
      if (DP[x + 1][y] < DP[x][y] + abs(no - x) * act) {
        DP[x + 1][y] = DP[x][y] + abs(no - x) * act;
      }
      if (DP[x][y + 1] < DP[x][y] + abs(no - (N - 1 - y)) * act) {
        DP[x][y + 1] = DP[x][y] + abs(no - (N - 1 - y)) * act;
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= N; i++) {
    if (ans < DP[i][N - i]) {
      ans = DP[i][N - i];
    }
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <cfenv>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <forward_list>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define MOD 1000000009
#define SIZE 101
typedef long long ll;
using namespace std;

int main() {
  ll N;
  vector<pair<ll, ll>> A;
  cin >> N;
  for (ll i = 0; i < N; i++) {
    ll tmp;
    cin >> tmp;
    A.push_back({tmp, i});
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  ll DP[SIZE][SIZE];
  for (int i = 0; i < SIZE; i++) {
    for (int j = 0; j < SIZE; j++) {
      DP[i][j] = 0;
    }
  }
  for (int i = 0; i < N; i++) {
    ll act = A[i].first;
    ll no = A[i].second;
    for (int x = 0; x <= i; x++) {
      int y = i - x;
      if (DP[x + 1][y] < DP[x][y] + abs(no - x) * act) {
        DP[x + 1][y] = DP[x][y] + abs(no - x) * act;
      }
      if (DP[x][y + 1] < DP[x][y] + abs(no - (N - 1 - y)) * act) {
        DP[x][y + 1] = DP[x][y] + abs(no - (N - 1 - y)) * act;
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= N; i++) {
    if (ans < DP[i][N - i]) {
      ans = DP[i][N - i];
    }
  }
  cout << ans;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

long int M = 1e+9 + 7;
int main(void) {
  int n;
  cin >> n;
  vi a(n), mx(n);
  rep(i, n) { cin >> a[i]; }
  vi a_cp(n);
  a_cp = a;
  rep(i, n) {
    int mxj = 0;
    srep(j, 1, n) {
      if (a_cp[j] > a_cp[mxj])
        mxj = j;
    }
    mx[i] = mxj;
    a_cp[mxj] = 0;
  }
  /*
  rep(i,n){
      cout << a[mx[i]] << endl;
  }
  */
  vvl dp(n + 1, vl(n + 1));
  rep(i, n) {
    int pi = mx[i];
    rep(l, i + 1) {
      int r = i - l;
      chmax(dp[i + 1][l], dp[i][l] + ll((n - r - 1) - pi) * a[pi]);
      dp[i + 1][l + 1] = dp[i][l] + ll(pi - l) * a[pi];
    }
  }
  /*
  rep(i,dp.size()){
      rep(j,dp[i].size()){
          cout << dp[i][j] << " ";
      }
      cout << endl;
  }
  */
  ll ans = 0;
  rep(i, n + 1) chmax(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

long int M = 1e+9 + 7;
int main(void) {
  int n;
  cin >> n;
  vi a(n), mx(n);
  rep(i, n) { cin >> a[i]; }
  vi a_cp(n);
  a_cp = a;
  rep(i, n) {
    int mxj = 0;
    srep(j, 1, n) {
      if (a_cp[j] > a_cp[mxj])
        mxj = j;
    }
    mx[i] = mxj;
    a_cp[mxj] = 0;
  }
  /*
  rep(i,n){
      cout << a[mx[i]] << endl;
  }
  */
  vvl dp(n + 1, vl(n));
  rep(i, n) {
    int pi = mx[i];
    rep(l, i + 1) {
      int r = i - l;
      chmax(dp[i + 1][l + 1], dp[i][l] + ll(pi - l) * a[pi]);
      chmax(dp[i + 1][l], dp[i][l] + ll((n - r - 1) - pi) * a[pi]);
    }
  }
  ll ans = 0;
  rep(i, n + 1) chmax(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 1000000007;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n;
  cin >> n;
  vector<P> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].fs;
    a[i].sc = i;
  }
  sort(RALL(a));
  vector<vector<LL>> dp(n + 1, vector<LL>(n + 1, -LINF));
  dp[0][n - 1] = 0;
  for (int i = 0; i < n; i++) {
    for (int l = 0; l <= i; l++) {
      int r = n - (i - l) - 1;
      if (l + 1 <= n)
        chmax(dp[l + 1][r], dp[l][r] + a[i].fs * abs(a[i].sc - l));
      if (r - 1 >= 0)
        chmax(dp[l][r - 1], dp[l][r] + a[i].fs * abs(a[i].sc - r));
    }
  }
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    chmax(ans, dp[i][i - 1]);
    chmax(ans, dp[i - 1][i]);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 1000000007;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n;
  cin >> n;
  vector<P> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].fs;
    a[i].sc = i;
  }
  sort(RALL(a));
  vector<vector<LL>> dp(n + 1, vector<LL>(n + 1, -LINF));
  dp[0][n - 1] = 0;
  for (int i = 0; i < n; i++) {
    for (int l = 0; l <= i; l++) {
      int r = n - (i - l) - 1;
      chmax(dp[l + 1][r], dp[l][r] + a[i].fs * abs(a[i].sc - l));
      chmax(dp[l][r - 1], dp[l][r] + a[i].fs * abs(a[i].sc - r));
    }
  }
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    chmax(ans, dp[i][i - 1]);
    chmax(ans, dp[i - 1][i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  REP(i, n) {
    cin >> a[i].fi;
    a[i].se = i;
  }
  sort(rALL(a));
  vvll dp(n + 1, vll(n + 1, 0LL));
  REP(i, n) {
    REP(j, i + 1) {
      dp[i + 1][j] = max(dp[i + 1][j],
                         dp[i][j] + a[i].fi * abs(n - 1 - (i - j) - a[i].se));
      dp[i + 1][j + 1] =
          max(dp[i + 1][j + 1], dp[i][j] + a[i].fi * abs(a[i].se - j));
    }
  }
  ll ans = 0;
  REP(i, n + 1) { ans = max(ans, dp[n][i]); }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define mod (ll)(1e9 + 7)
#define FIX(a) ((a) % mod + mod) % mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define fi first
#define se second
#define pb push_back
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  REP(i, n) {
    cin >> a[i].fi;
    a[i].se = i;
  }
  sort(rALL(a));
  vvll dp(n + 1, vll(n, 0LL));
  REP(i, n) {
    REP(j, i + 1) {
      dp[i + 1][j] = max(dp[i + 1][j],
                         dp[i][j] + a[i].fi * abs(n - 1 - (i - j) - a[i].se));
      dp[i + 1][j + 1] =
          max(dp[i + 1][j + 1], dp[i][j] + a[i].fi * abs(a[i].se - j));
    }
  }
  ll ans = 0;
  REP(i, n) { ans = max(ans, dp[n][i]); }
  cout << ans << endl;

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2000;
inline long long read() // inline 加速读入
{
  long long x = 0;
  char c = getchar(); // x代表返回值，c代表读取的字符
  while (c < '0' || c > '9')
    c = getchar(); // 读取所有非数部分
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar(); // 如果读取的字符为数，加入返回值
  return x;
}
ll n, k, t, aa[MAXN + 10][MAXN + 10];
struct hh {
  ll x;
  int y;
} a[MAXN + 10];
int cmp(hh a, hh y) { return a.x > y.x; }
ll run(int l, int r) {
  if (l > r)
    return 0;
  if (aa[l][r])
    return aa[l][r];
  int i = n - (r - l);
  return aa[l][r] = max(run(l, r - 1) + a[i].x * (r - a[i].y),
                        run(l + 1, r) + a[i].x * (a[i].y - l));
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    a[i].x = read();
    a[i].y = i;
  }
  sort(a + 1, a + n + 1, cmp);
  run(1, n);
  cout << aa[1][n] << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2000;
inline long long read() // inline 加速读入
{
  long long x = 0;
  char c = getchar(); // x代表返回值，c代表读取的字符
  while (c < '0' || c > '9')
    c = getchar(); // 读取所有非数部分
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar(); // 如果读取的字符为数，加入返回值
  return x;
}
ll n, k, t, aa[MAXN][MAXN];
struct hh {
  ll x;
  int y;
} a[MAXN + 10];
int cmp(hh a, hh y) { return a.x > y.x; }
ll run(int l, int r) {
  if (l > r)
    return 0;
  if (aa[l][r])
    return aa[l][r];
  int i = n - (r - l);
  return aa[l][r] = max(run(l, r - 1) + a[i].x * (r - a[i].y),
                        run(l + 1, r) + a[i].x * (a[i].y - l));
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].x;
    a[i].y = i;
  }
  sort(a + 1, a + n, cmp);
  run(1, n);
  cout << aa[1][n] << endl;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long
#define F first
#define S second
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);
const int N = 2005;
int dp[N][N];
int n;
vector<pair<int, int>> a;
int solve(int l, int r) {
  if (l > r)
    return 0;
  int &res = dp[l][r];
  if (res != -1)
    return res;
  int id = n - (r - l + 1);
  int pos = a[id].S;
  int val = a[id].F;
  res = max(res, solve(l + 1, r) + abs(pos - l) * val);
  res = max(res, solve(l, r - 1) + abs(pos - r) * val);
  return res;
}
int32_t main() {
  fast;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].F;
    a[i].S = i;
  }
  sort(a.rbegin(), a.rend());
  memset(dp, -1, sizeof(dp));
  cout << solve(0, n - 1);
}

Error: WA

Fixed submission:

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

#define int long long
#define F first
#define S second
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);
const int N = 1005;
int dp[N][N];
int n;
vector<pair<int, int>> a;
int solve(int l, int r) {
  if (l > r)
    return 0;
  int &res = dp[l][r];
  if (res != -1)
    return res;
  int id = n - (r - l + 1);
  int pos = a[id].S;
  int val = a[id].F;
  res = max(res, solve(l + 1, r) + abs(pos - l) * val);
  res = max(res, solve(l, r - 1) + abs(pos - r) * val);
  return res;
}
int32_t main() {
  fast;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].F;
    a[i].S = i;
  }
  sort(a.rbegin(), a.rend());
  memset(dp, -1, sizeof(dp));
  cout << solve(0, n - 1);
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define simplify                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define endl "\\n"
using namespace std;

vector<pair<int, int>> arr;
int dp[2005][2005];
int n;
int solve(int pos, int left, int right) {
  if (pos >= n)
    return 0;
  if (left > right)
    return -1e10;
  int &ans = dp[left][right];

  if (ans != -1)
    return ans;
  int maxi1 = solve(pos + 1, left + 1, right) +
              abs(arr[pos].second - left) * arr[pos].first;
  int maxi2 = solve(pos + 1, left, right - 1) +
              abs(arr[pos].second - right) * arr[pos].first;

  return ans = max(maxi1, maxi2);
}

int32_t main() {

  simplify cin >> n;
  arr.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i].first;
    arr[i].second = i;
  }
  sort(arr.rbegin(), arr.rend());
  memset(dp, -1, sizeof dp);
  int ans = solve(0, 0, n - 1);
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define simplify                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define endl "\\n"
using namespace std;

vector<pair<int, int>> arr;
int dp[2005][2005];
int n;
int solve(int pos, int left, int right) {
  if (pos >= n)
    return 0;
  if (left > right)
    return -1e10;
  int &ans = dp[left][right];

  if (ans != -1)
    return ans;
  int maxi1 = solve(pos + 1, left + 1, right) +
              abs(arr[pos].second - left) * arr[pos].first;
  int maxi2 = solve(pos + 1, left, right - 1) +
              abs(arr[pos].second - right) * arr[pos].first;

  return max(maxi1, maxi2);
}

int32_t main() {

  simplify cin >> n;
  arr.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i].first;
    arr[i].second = i;
  }
  sort(arr.rbegin(), arr.rend());
  memset(dp, -1, sizeof dp);
  int ans = solve(0, 0, n - 1);
  cout << ans;

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
int N;
ll A[2000];
P p[2000];
ll dp[2005][2005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    p[i] = P(A[i], i);
  }
  sort(p, p + N, greater<P>());
  for (int i = 0; i < N; i++) {
    ll a = p[i].first;
    int idx = p[i].second;
    for (int j = 0; j <= i; j++) {
      int l = i - j;
      int r = N - j - 1;
      // left
      if (i >= 1)
        dp[i][j] = max(dp[i][j], dp[i - 1][j] + abs(idx - l) * a);
      else
        dp[i][j] = abs(idx - l) * a;
      // right
      if (i >= 1)
        dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + abs(r - idx) * a);
      else
        dp[i][j + 1] = abs(r - idx) * a;
    }
  }
  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    ans = max(ans, dp[N - 1][i]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
int N;
ll A[2000];
P p[2000];
ll dp[2005][2005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    p[i] = P(A[i], i);
  }
  sort(p, p + N, greater<P>());
  for (int i = 0; i < N; i++) {
    ll a = p[i].first;
    int idx = p[i].second;
    for (int j = 0; j <= i; j++) {
      int l = i - j;
      int r = N - j - 1;
      // left
      dp[i][j] = max(dp[i][j], dp[i - 1][j] + abs(idx - l) * a);
      // right
      dp[i][j + 1] = max(dp[i][j + 1], dp[i - 1][j] + abs(r - idx) * a);
    }
  }
  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    ans = max(ans, dp[N - 1][i]);
  }
  cout << ans << endl;
}