alexjercan/bugnet huggingface dataset:
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ms(x, y) memset(x, y, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define sqr(x) ((x) * (x))
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

void run_case() {
  int n;
  cin >> n;
  vector<pll> a;
  for (int i = 1; i <= n; ++i) {
    LL t;
    cin >> t;
    a.emplace_back(t, 1LL * i);
  }
  sort(a.begin(), a.end(), [&](pll a, pll b) { return a.first > b.first; });
  vector<vector<LL>> dp(n + 2, vector<LL>(n + 2));
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= i; ++j) { // len = r - l + 1, len = n - i
      int l = j + 1, r = n - i + l - 1;
      if (r + 1 <= n)
        dp[l][r] = max(dp[l][r], dp[l][r + 1] + abs(r + 1 - a[i - 1].second) *
                                                    a[i - 1].first);
      if (l - 1 >= 1)
        dp[l][r] = max(dp[l][r], dp[l - 1][r] + abs(l - 1 - a[i - 1].second) *
                                                    a[i - 1].first);
    }
  }
  LL ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, dp[i][i - 1]);
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cout.flags(ios::fixed);
  cout.precision(9);
  // int t; cin >> t;
  // while(t--)
  run_case();
  cout.flush();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ls(x) x << 1
#define rs(x) x << 1 | 1
#define low(x) (x & -x)
#define mp make_pair
#define X first
#define Y second
#ifdef _DEBUG
const int N = 1e3 + 10;
#else
const int N = 2e3 + 10;
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef double db;
const ll mod = 1e9 + 7;
template <typename T> inline T gcd(T a, T b) { return !b ? a : gcd(b, a % b); }
template <typename T> inline T q_pow(T a, T x) {
  T ans = 1, tmp = a;
  while (x) {
    if (x & 1)
      (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    x >>= 1;
  }
  return ans;
}
template <typename T> inline void re(T &N) {
  int f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      f = -1;
  N = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    N = N * 10 + c - '0';
  N *= f;
}
int m, n, t = 1;
ll d[N][N];
pl a[N];
ll dfs(int l, int r) {
  if (l == r)
    return a[1].X * abs(a[1].Y - l);
  d[l][r] =
      max(d[l][r], dfs(l + 1, r) + abs(l - a[r - l + 1].Y) * a[r - l + 1].X);
  d[l][r] =
      max(d[l][r], dfs(l, r - 1) + abs(r - a[r - l + 1].Y) * a[r - l + 1].X);
  return d[l][r];
}
int main() {
  // freopen("data.txt","r",stdin);
  memset(d, -1, sizeof(d));
  re(n);
  for (int i = 1; i <= n; i++)
    re(a[i].X), a[i].Y = i;
  sort(a + 1, a + n + 1);
  printf("%lld\\n", dfs(1, n));
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
#define clr(c) memset(c, 0, sizeof(c))
using namespace std;
int mod = 1e9 + 7;
const int M = 1e6 + 10;
const int N = 2e3 + 10;
inline ll read() {
  ll b = 1, sum = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      b = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return b * sum;
}
struct node {
  int loc;
  ll val;
} a[N];
bool cmp(node x, node y) { return x.val > y.val; }
int n, m, k, T;
ll ans;
ll dp[N][N];
int main() {
  int i, j;
  n = read();
  for (i = 1; i <= n; i++) {
    a[i].val = read();
    a[i].loc = i;
  }
  sort(a + 1, a + n + 1, cmp);
  int l = 1, r = n;
  for (i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] + a[i].val * abs((n - i + 1) - a[i].loc);
    for (j = 1; j <= i - 1; j++) {
      l = j;
      r = n - (i - j - 1);
      dp[i][j] = max(dp[i - 1][j - 1] + a[i].val * abs(a[i].loc - l),
                     dp[i - 1][j] + a[i].val * abs(r - a[i].loc));
    }
    dp[i][i] = dp[i - 1][i - 1] + a[i].val * abs(a[i].loc - i);
  }
  for (i = 0; i <= n; i++) {
    ans = max(ans, dp[n][i]);
  }
  printf("%lld", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
#define clr(c) memset(c, 0, sizeof(c))
using namespace std;
int mod = 1e9 + 7;
const int M = 1e6 + 10;
const int N = 2e3 + 10;
inline ll read() {
  ll b = 1, sum = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      b = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return b * sum;
}
struct node {
  int loc;
  ll val;
} a[N];
int n, m, k, T;
ll ans;
ll dp[N][N];
bool cmp(node x, node y) { return x.val > y.val; }
ll dfs(ll res, int k, int l, int r) {
  if (k == n + 1) {
    ans = max(ans, res);
    return 0;
  }
  if (dp[l][r] != 0) {
    res += dp[l][r];
    ans = max(ans, res);
    return dp[l][r];
  }
  ll x = 0;
  x = max(x, dfs(res + a[k].val * (a[k].loc - l), k + 1, l + 1, r));
  x = max(x, dfs(res + a[k].val * (r - a[k].loc), k + 1, l, r - 1));
  dp[l][r] = x;
  return x;
}
int main() {
  int i, j;
  n = read();
  for (i = 1; i <= n; i++) {
    a[i].val = read();
    a[i].loc = i;
  }
  sort(a + 1, a + n + 1, cmp);
  int l = 1, r = n;
  dfs(0, 1, 1, n);

  printf("%lld", ans);
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error


// E - Active Infants

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

pair<ll, int> A[2000]; // 活発度とindexのペア
ll dp[2001][2001];     // dp[l][r]:
                   // 左端にl人、右端にr人移動させたときのうれしさの合計の最大値

int main() {
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A[i] = make_pair(a, i);
  }

  sort(A, A + N, greater<pair<ll, int>>());

  ll ans = 0;

  dp[0][0] = 0;

  for (int l = 0; l < N; l++) {
    for (int r = 0; r < N; r++) {
      int i = l + r;
      if (i >= N)
        continue;

      ll act = A[i].first;
      int loc = A[i].second;

      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + act * abs(l - loc));
      dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + act * abs((N - 1) - r - loc));

      ans = max(ans, dp[l + 1][r]);
      ans = max(ans, dp[l][r + 1]);
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:


// E - Active Infants

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

pair<ll, int> A[2000]; // 活発度とindexのペア
ll dp[2001][2001];     // dp[l][r]:
                   // 左端にl人、右端にr人移動させたときのうれしさの合計の最大値

int main() {
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A[i] = make_pair(a, i);
  }

  sort(A, A + N, greater<pair<ll, int>>());

  ll ans = 0;

  dp[0][0] = 0;

  for (int l = 0; l < N; l++) {
    for (int r = 0; r < N; r++) {
      int i = l + r;
      ll act = A[i].first;
      int loc = A[i].second;

      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + act * abs(l - loc));
      dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + act * abs((N - 1) - r - loc));

      ans = max(ans, dp[l + 1][r]);
      ans = max(ans, dp[l][r + 1]);
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef double db;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(all(a));
  reverse(all(a));
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= i + 1; ++j) {
      dp[j][i + 1 - j] = max(dp[j][i + 1 - j],
                             dp[j - 1][i + 1 - j] +
                                 1ll * a[i].first * abs(a[i].second + 1 - j));
      dp[i + 1 - j][j] = max(dp[i + 1 - j][j],
                             dp[i + 1 - j][j - 1] +
                                 1ll * a[i].first * abs(a[i].second - (n - j)));
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; ++i)
    ans = max(ans, dp[i][n - i]);
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef double db;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(all(a));
  reverse(all(a));
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= i + 1; ++j) {
      dp[j][i + 1 - j] = max(dp[j][i + 1 - j],
                             dp[j - 1][i + 1 - j] +
                                 1ll * a[i].first * abs(a[i].second + 1 - j));
      dp[i + 1 - j][j] = max(dp[i + 1 - j][j],
                             dp[i + 1 - j][j - 1] +
                                 1ll * a[i].first * abs(a[i].second - (n - j)));
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; ++i)
    ans = max(ans, dp[i][n - i]);
  cout << ans;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long int LL;

int n;
vector<LL> a;
vector<vector<LL>> dp;

LL solve() {
  if (n == 1)
    return 0;
  vector<pair<LL, LL>> order;
  dp = vector<vector<LL>>(n + 1, vector<LL>(n + 1, 0));
  for (int i = 0; i < n; i++)
    order.emplace_back(a[i], i);
  sort(order.begin(), order.end(), [](pair<LL, LL> L, pair<LL, LL> R) {
    if (L.first != R.first)
      return L.first > R.first;
    return L.second < R.second;
  });
  for (int i = 1; i <= n; i++) {
    dp[0][i] =
        dp[0][i - 1] + abs(order[i - 1].second - n + i) * order[i - 1].first;
    dp[i][0] =
        dp[i - 1][0] + abs(order[i - 1].second - i + 1) * order[i - 1].first;
  }
  for (int i = 2; i <= n; i++)
    for (int L = 1; L < i; L++) {
      int R = i - L;
      dp[L][R] =
          max(dp[L][R], max(dp[L - 1][R] + abs(order[i - 1].second - L + 1) *
                                               order[i - 1].first,
                            dp[L][R - 1] + abs(order[i - 1].second - n + R) *
                                               order[i - 1].first));
    }
  LL ans = dp[0][n];
  for (int L = 0; L <= n; L++)
    ans = max(ans, dp[L][n - L]);
  dp.clear();
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  while (cin >> n) {
    a = vector<LL>(n);
    for (auto &&x : a)
      cin >> x;
    cout << solve() << endl;
    a.clear();
  }
}

Error: 0

Fixed submission:

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

typedef long long int LL;

int n;
vector<LL> a;
vector<vector<LL>> dp;

LL solve() {
  if (n == 1)
    return 0;
  vector<pair<LL, LL>> order;
  dp = vector<vector<LL>>(n + 1, vector<LL>(n, 0));
  for (int i = 0; i < n; i++)
    order.emplace_back(a[i], i);
  sort(order.begin(), order.end(), [](pair<LL, LL> L, pair<LL, LL> R) {
    if (L.first != R.first)
      return L.first > R.first;
    return L.second < R.second;
  });
  for (int i = 1; i <= n; i++) {
    dp[0][i] =
        dp[0][i - 1] + abs(order[i - 1].second - n + i) * order[i - 1].first;
    dp[i][0] =
        dp[i - 1][0] + abs(order[i - 1].second - i + 1) * order[i - 1].first;
  }
  for (int i = 1; i <= n; i++)
    for (int L = 1; L < i; L++) {
      int R = i - L;
      dp[L][R] =
          max(dp[L][R], max(dp[L - 1][R] + abs(order[i - 1].second - L + 1) *
                                               order[i - 1].first,
                            dp[L][R - 1] + abs(order[i - 1].second - n + R) *
                                               order[i - 1].first));
    }
  LL ans = 0;
  for (int L = 0; L <= n; L++)
    ans = max(ans, dp[L][n - L]);
  dp.clear();
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  while (cin >> n) {
    a = vector<LL>(n);
    for (auto &&x : a)
      cin >> x;
    cout << solve() << endl;
    a.clear();
  }
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

// Powered by caibiCH2 at 2020/04/24,19:58:15
#include <bits/stdc++.h>
#define elif else if
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
ll f[2010][2010];
struct msp {
  ll num, data;
} a[2010];
inline ll cal(int cx, int wz) { return abs(wz - a[cx].num) * a[cx].data; }
signed main() {
  ios::sync_with_stdio(0);
  // cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].data;
    a[i].num = i;
  }
  sort(a + 1, a + n + 1,
       [](const msp &x, const msp &y) { return x.data > y.data; });
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n - i; ++j) {
      int now = i + j;
      if (i == 0 && j == 0)
        continue;
      elif (i == 0) { f[i][j] = f[i][j - 1] + cal(now, n - j + 1); }
      elif (j == 0) { f[i][j] = f[i - 1][j] + cal(now, i); }
      else {
        f[i][j] =
            max(f[i][j - 1] + cal(now, n - j + 1), f[i - 1][j] + cal(now, i));
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= n; ++i) {
    ans = max(ans, f[i][n - i]);
  }
  cout << ans << '\\n';
  return 0;
}

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 {
  int x, 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 * abs(r - a[i].y),
                        run(l + 1, r) + a[i].x * abs(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, cmp);
  run(1, n);
  cout << aa[1][n] << endl;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct Child {
  long long A, place;
  bool operator>(Child XYZ) const { return A > XYZ.A; }
};
long long dp[2002][2002];
int main() {
  long long N;
  cin >> N;
  vector<Child> in(N);
  for (long long i = 0; i < N; i++) {
    cin >> in[i].A;
    in[i].place = i;
  }
  sort(in.begin(), in.end(), greater<Child>());
  long long ans = 0;
  for (long long l = 0; l < N; l++) {
    for (long long r = 0; r < N; r++) {
      long long tmp = l + r;
      if (l + r == N) {
        ans = max(ans, dp[l][r]);
        break;
      }
      dp[l + 1][r] =
          max(dp[l][r] + abs(in[tmp].place - l) * in[tmp].A, dp[l + 1][r]);
      dp[l][r + 1] =
          max(dp[l][r] + abs(in[tmp].place - (N - 1 - r)) * in[tmp].A,
              dp[l][r + 1]);
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
struct node {
  int id;
  int v;
  bool operator<(const node &b) const { return v > b.v; }
} e[2000 + 50];
int n;
long long ABS(long long x) { return x < 0 ? 0 - x : x; }
long long dfs(int l, int r, int i) {
  if (i > n)
    return 0;
  long long tp = e[i].v;
  long long k = tp * ABS(l - e[i].id);
  long long j = tp * ABS(r - e[i].id);
  long long re = 0;
  re = max(k + dfs(l + 1, r, i + 1), j + dfs(l, r - 1, i + 1));
  //  cout<<re<<' '<<l<<' '<<r<<' '<<i<<'\\n';
  return re;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &e[i].v);
    e[i].id = i;
  }
  sort(e + 1, e + 1 + n);
  int l = 1, r = n;
  long long ans = 0;
  ans = dfs(1, n, 1);
  cout << ans;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
typedef vector<int> vi;
const int INF = 1LL << 60; // 10e18+10e17ちょっとくらい
const int MOD = 1000000007;
const int MAX_N = 200100;

int dp[2020][2020];
signed main() {
  //  以降 cin の入力元が 'input.txt' になる
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  int N;
  cin >> N;

  vector<pair<int, int>> q;
  REP(i, N) {
    int tmp;
    cin >> tmp;
    q.emplace_back(make_pair(tmp, i));
  }
  sort(ALL(q));
  reverse(ALL(q));
  int ans = 0;
  REP(i, N) {
    for (int l = 0; l <= i; l++) {
      int r = i - l;
      dp[l + 1][r] =
          max(dp[l + 1][r], dp[l][r] + q[i].first * abs(q[i].second - l));
      dp[l][r + 1] = max(
          dp[l][r + 1], dp[l][r] + q[i].first * abs(q[i].second - (N - 1 - r)));
      ans = max(ans, dp[l + 1][r]);
      ans = max(ans, dp[l][r + 1]);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
typedef vector<int> vi;
const int INF = 1LL << 60; // 10e18+10e17ちょっとくらい
const int MOD = 1000000007;
const int MAX_N = 200100;

int dp[2020][2020];
signed main() {
  //  以降 cin の入力元が 'input.txt' になる
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  int N;
  cin >> N;

  vector<pair<int, int>> q;
  REP(i, N) {
    int tmp;
    cin >> tmp;
    q.emplace_back(make_pair(tmp, i));
  }
  sort(ALL(q));
  reverse(ALL(q));
  queue<pair<int, int>> que;
  que.push(make_pair(0, 0));
  int ans = 0;
  while (!que.empty()) {
    int l = que.front().first;
    int r = que.front().second;
    que.pop();
    int idx = l + r;
    if (idx < N) {
      dp[l + 1][r] =
          max(dp[l + 1][r], dp[l][r] + q[idx].first * abs(q[idx].second - l));
      dp[l][r + 1] =
          max(dp[l][r + 1],
              dp[l][r] + q[idx].first * abs(q[idx].second - (N - 1 - r)));
      ans = max(ans, dp[l + 1][r]);
      ans = max(ans, dp[l][r + 1]);
      que.push(make_pair(l + 1, r));
      que.push(make_pair(l, r + 1));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int max_n = 2005;
ll dp[max_n][max_n];
inline void chmax(ll &a, ll b) { a = max(a, b); }

int main() {
  int n;

  cin >> n;

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

  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < n + 1; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;

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

  for (int i = 0; i < n; i++) {
    int pi = p[i].second;
    for (int l = 0; l < i + 1; l++) {
      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;
  for (int i = 0; i < n + 1; i++) {
    chmax(ans, dp[n][i]);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int max_n = 2005;

void chmax(ll &a, ll b) { a = max(a, b); }

int main() {
  int n;
  ll dp[max_n][max_n];

  cin >> n;

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; i < n; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;

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

  for (int i = 0; i < n; i++) {
    int pi = p[i].second;
    for (int l = 0; l < i + 1; l++) {
      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;
  for (int i = 0; i < n + 1; i++) {
    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;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define vcout(v)                                                               \\
  rep(i, v.size()) cout << v[i] << " ";                                        \\
  cout << endl
#define mcout(m)                                                               \\
  rep(j, m.size()) { vcout(m[j]); }
const ll MOD = 1e9 + 7;

int mainsolve() {

  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  vector<P> ps(n);
  rep(i, n) ps[i] = P(a[i], i);
  sort(ps.rbegin(), ps.rend());

  ll INF = 1e18;
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -INF));
  rep(i, n + 1) dp[0][i] = 0;

  rep(i, n) {
    rep(j, n + 1) {
      if (i < j)
        continue;
      dp[i + 1][j] =
          max(dp[i + 1][j],
              dp[i][j] + ps[i].first * (n - (i - j) - 1 - ps[i].second));
      if (j + 1 <= n)
        dp[i + 1][j + 1] =
            max(dp[i + 1][j + 1], dp[i][j] + ps[i].first * (ps[i].second - j));
    }
  }

  ll res = 0;
  rep(i, n + 1) res = max(res, dp[n][i]);
  cout << res << endl;
  return 0;
}

int main() { return mainsolve(); }

Error: -6
c4f96e44-570b-429f-b59e-ac5025300cf7.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02709/C++/s108658118.cpp:31: int mainsolve(): Assertion `dp[i+1][j+1] >= 0' failed.


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define vcout(v)                                                               \\
  rep(i, v.size()) cout << v[i] << " ";                                        \\
  cout << endl
#define mcout(m)                                                               \\
  rep(j, m.size()) { vcout(m[j]); }
const ll MOD = 1e9 + 7;

int mainsolve() {

  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  vector<P> ps(n);
  rep(i, n) ps[i] = P(a[i], i);
  sort(ps.rbegin(), ps.rend());

  ll INF = 1e18;
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -INF));
  rep(i, n + 1) dp[0][i] = 0;

  rep(i, n) {
    rep(j, n) {
      ll chooseL = 0, chooseR = 0;
      if (i >= j + 1)
        chooseR = dp[i][j + 1] + ps[i].first * (n - (i - j) - ps[i].second);
      if (i >= j)
        chooseL = dp[i][j] + ps[i].first * (ps[i].second - j);
      if (i + 1 >= j + 1)
        dp[i + 1][j + 1] = max(chooseL, chooseR);
      assert(dp[i + 1][j + 1] >= 0);
    }
  }

  ll res = 0;
  rep(i, n + 1) res = max(res, dp[n][i]);
  cout << res << endl;
  return 0;
}

int main() { return mainsolve(); }