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

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

#define endl '\\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ends << H;
  debug_out(T...);
}
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int N = 2e3 + 5;

int A[N], P[N];
ll ans, DP[N][N];

bool CMP(int i, int j) { return A[i] > A[j]; }

int main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  mt19937 Rnd(time(0));

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i], P[i] = i;
  sort(P, P + n, CMP);
  for (int x = 0; x <= n; x++)
    for (int y = 0; x + y <= n; y++) {
      if (!x && !y)
        continue;
      int i = x + y - 1;
      if (x)
        DP[x][y] =
            max(DP[x][y], DP[x - 1][y] + 1LL * A[P[i]] * ((P[i] + 1) - x));
      if (y)
        DP[x][y] = max(DP[x][y], DP[x][y - 1] + 1LL * A[P[i]] *
                                                    ((n - y + 1) - (P[i] + 1)));
      if (x + y == n)
        ans = max(ans, DP[x][y]);
    }
  cout << ans << endl;

  return 0;
}


Error: WA

Fixed submission:

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

#define endl '\\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ends << H;
  debug_out(T...);
}
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int N = 2e3 + 5;

int A[N], P[N];
ll ans, DP[N][N];

bool CMP(int i, int j) { return A[P[i]] > A[P[j]]; }

int main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  mt19937 Rnd(time(0));

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i], P[i] = i;
  sort(P, P + n, CMP);
  for (int x = 0; x <= n; x++)
    for (int y = 0; x + y <= n; y++) {
      if (!x && !y)
        continue;
      int i = x + y - 1;
      if (x && P[i] > x)
        DP[x][y] =
            max(DP[x][y], DP[x - 1][y] + 1LL * A[P[i]] * ((P[i] + 1) - x));
      if (y && n - y >= P[i])
        DP[x][y] = max(DP[x][y], DP[x][y - 1] + 1LL * A[P[i]] *
                                                    ((n - y + 1) - (P[i] + 1)));
      if (x + y == n)
        ans = max(ans, DP[x][y]);
    }
  cout << ans << endl;

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

#include <bits/stdc++.h>

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (1 << 29)

static const int MAX = 3000;

using namespace std;

int main() {
  int n;
  cin >> n;
  long dp[MAX][MAX];
  vector<pair<int, int>> A;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.pb(make_pair(a, i));
  }

  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  long m = 0;

  for (long i = 0; i <= n; i++)
    for (long j = 0; j <= n; j++)
      dp[i][j] = 0;

  for (long l = 0; l <= n; l++) {
    for (long r = 0; r <= n; r++) {
      if (l + r == n) {
        m = max(m, dp[l][r]);
        break;
      }

      long act = A[l + r].first, idx = A[l + r].second;

      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + act * abs(idx - l));
      dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + act * abs(idx - (n - 1 - r)));
    }
  }

  /*for(int i = 0; i <= n; i++){
          for(int j = 0; j <= n; j++){
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/
  cout << m << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (1 << 29)

static const int MAX = 500;

using namespace std;

int main() {
  int n;
  cin >> n;
  int dp[MAX][MAX];
  vector<pair<int, int>> A;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.pb(make_pair(i, a));
  }

  sort(A.begin(), A.end());

  int m = 0;

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = 0;

  for (int l = 0; l <= n; l++) {
    for (int r = 0; r <= n; r++) {
      if (l + r == n) {
        m = max(m, dp[l][r]);
        break;
      }

      int act = A[l + r].second, idx = A[l + r].first;

      dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + act * abs(l - idx));
      dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + act * abs(n - 1 - r - idx));
    }
  }

  /*for(int i = 0; i <= n; i++){
          for(int j = 0; j <= n; j++){
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/
  cout << m << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 2020;
int n;
int a[N], p[N];
ll dp[N][N];

ll f(int l, int r) {
  int dep = l + r + 1;
  if (dep > n) {
    return 0;
  }
  if (dp[l][r] != -1)
    return dp[l][r];
  if (dep == n) {
    return dp[l][r] = 1LL * a[p[n]] * abs(l + 1 - p[n]);
  }
  ll res = max(1LL * a[p[dep]] * abs(l + 1 - p[dep]) + f(l + 1, r),
               1LL * a[p[dep]] * abs(n - r - p[dep]) + f(l, r + 1));
  return dp[l][r] = res;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    p[i] = i;
  }
  sort(p + 1, p + n + 1,
       [&](const int &i, const int &j) { return a[i] > a[j]; });
  memset(dp, -1, sizeof(dp));
  printf("%lld\\n", f(0, 0));
  return 0;
}

Error: TLE

Fixed submission:

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

const int N = 2020;
int n;
int a[N], p[N];

ll dp(int l, int r) {
  int dep = l + r + 1;
  if (dep > n) {
    return 0;
  }
  if (dep == n) {
    return 1LL * a[p[n]] * abs(l + 1 - p[n]);
  }
  ll res = max(1LL * a[p[dep]] * abs(l + 1 - p[dep]) + dp(l + 1, r),
               1LL * a[p[dep]] * abs(n - r - p[dep]) + dp(l, r + 1));
  return res;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    p[i] = i;
  }
  sort(p + 1, p + n + 1,
       [&](const int &i, const int &j) { return a[i] > a[j]; });
  printf("%lld\\n", dp(0, 0));
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdint>
#include <functional>
#include <iostream>
#include <utility>
#include <vector>

template <typename F> constexpr decltype(auto) fix(F &&f) {
  return [f = std::forward<F>(f)](auto &&...args) {
    return f(f, std::forward<decltype(args)>(args)...);
  };
}

using i64 = std::int64_t;

int main() {
  int n;
  std::cin >> n;

  std::vector<std::pair<i64, int>> vec(n);
  for (int i = 0; i < n; ++i) {
    i64 a;
    std::cin >> a;

    vec[i] = {a, i};
  }

  std::sort(vec.begin(), vec.end(), std::greater<>{});

  std::vector memo(n, std::vector<i64>(n, -1));
  auto solve = fix([&](auto f, int idx, int l) -> i64 {
    if (idx == n)
      return 0;

    if (memo[idx][l] != -1)
      return memo[idx][l];

    int r = l + n - 1 - idx;
    int ld = std::abs(vec[idx].second - l);
    int rd = std::abs(vec[idx].second - r);

    i64 ret = 0;

    ret = std::max(ret, vec[idx].first * ld + f(f, idx + 1, l + 1));
    ret = std::max(ret, vec[idx].first * rd + f(f, idx + 1, l));

    return memo[idx][l] = ret;
  });

  std::cout << solve(0, 0) << std::endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdint>
#include <functional>
#include <iostream>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

template <typename F> constexpr decltype(auto) fix(F &&f) {
  return [f = std::forward<F>(f)](auto &&...args) {
    return f(f, std::forward<decltype(args)>(args)...);
  };
}

namespace std {
template <> struct hash<std::pair<int, int>> {
  size_t operator()(const std::pair<int, int> &v) const {
    return std::hash<int>()(v.first) ^ std::hash<int>()(v.second);
  }
};
} // namespace std

using i64 = std::int64_t;

int main() {
  int n;
  std::cin >> n;

  std::vector<std::pair<i64, int>> vec(n);
  for (int i = 0; i < n; ++i) {
    i64 a;
    std::cin >> a;

    vec[i] = {a, i};
  }

  std::sort(vec.begin(), vec.end(), std::greater<>{});

  std::unordered_map<std::pair<int, int>, i64> memo;
  auto solve = fix([&](auto f, int idx, int l) -> i64 {
    if (idx == n)
      return 0;

    if (memo.count({idx, l}) != 0)
      return memo[{idx, l}];

    int r = l + n - 1 - idx;
    int ld = std::abs(vec[idx].second - l);
    int rd = std::abs(vec[idx].second - r);

    i64 ret = 0;

    ret = std::max(ret, vec[idx].first * ld + f(f, idx + 1, l + 1));
    ret = std::max(ret, vec[idx].first * rd + f(f, idx + 1, l));

    return memo[{idx, l}] = ret;
  });

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

vector<P> vp;
ll dp[2010][2010];

int main() {
  ll n;
  cin >> n;
  rep(i, n) {
    ll a;
    cin >> a;
    vp.push_back({a, i});
  }
  sort(vp.begin(), vp.end());
  reverse(vp.begin(), vp.end());

  rep(i, n + 1) {
    for (int j = 0; j + i <= n; j++) {
      ll cl = vp[i + j].first * (vp[i + j].second - i);
      ll cr = vp[i + j].first * (n - j - 1 - vp[i + j].second);
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + cl);
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + cr);
    }
  }

  ll ans = 0;
  rep(i, n) ans = max(ans, dp[i][n - i]);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

vector<P> vp;
ll dp[1010][1010];

int main() {
  ll n;
  cin >> n;
  rep(i, n) {
    ll a;
    cin >> a;
    vp.push_back({a, i});
  }
  sort(vp.begin(), vp.end());
  reverse(vp.begin(), vp.end());

  rep(i, n + 1) {
    for (int j = 0; j + i <= n; j++) {
      ll cl = vp[i + j].first * (vp[i + j].second - i);
      ll cr = vp[i + j].first * (n - j - 1 - vp[i + j].second);
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + cl);
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + cr);
    }
  }

  ll ans = 0;
  rep(i, n) ans = max(ans, dp[i][n - i]);

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> P;
int main() {
  int N;
  cin >> N;
  vector<P> a(N);
  for (int i = 0; i < N; i++) {
    long long b;
    cin >> b;
    a[i] = P(b, i);
  }
  sort(a.begin(), a.end());
  long long dp[N + 1][N + 1];
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      dp[i][j] = 0;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      int k = i - j;
      if (j == 0)
        dp[j][k] = dp[j][k - 1] + abs(N - k - a[N - i].second) * a[N - i].first;
      else if (k == 0)
        dp[j][k] = dp[j - 1][k] + abs(j - 1 - a[N - i].second) * a[N - i].first;
      else {
        long long m1 =
            dp[j][k - 1] + abs(N - k - a[N - i].second) * a[N - i].first;
        long long m2 =
            dp[j - 1][k] + abs(j - 1 - a[N - i].second) * a[N - i].first;
        dp[j][k] = max(m1, m2);
      }
    }
  }
  for (int i = 0; i <= N; i++)
    ans = max(ans, dp[i][N - i]);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> P;
int main() {
  int N;
  cin >> N;
  vector<P> a(N);
  for (int i = 0; i < N; i++) {
    long long b;
    cin >> b;
    a[i] = P(b, i);
  }
  sort(a.begin(), a.end());
  long long dp[N][N];
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      dp[i][j] = 0;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= i; j++) {
      int k = i - j;
      if (j == 0)
        dp[j][k] = dp[j][k - 1] + abs(N - k - a[N - i].second) * a[N - i].first;
      else if (k == 0)
        dp[j][k] = dp[j - 1][k] + abs(j - 1 - a[N - i].second) * a[N - i].first;
      else {
        long long m1 =
            dp[j][k - 1] + abs(N - k - a[N - i].second) * a[N - i].first;
        long long m2 =
            dp[j - 1][k] + abs(j - 1 - a[N - i].second) * a[N - i].first;
        dp[j][k] = max(m1, m2);
      }
    }
  }
  for (int i = 0; i <= N; i++)
    ans = max(ans, dp[i][N - i]);
  cout << ans << endl;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node {
  ll v;
  ll weizhi;
};
Node a[5005];
bool cmp(Node a, Node b) {
  if (a.v > b.v)
    return true;
  else if (a.v == b.v && a.weizhi < b.weizhi)
    return true;
  else
    return false;
}
ll dp[5005][5005];
long long ans;
ll zuo;
ll you;
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
int main() {
  int n;
  cin >> n;
  zuo = 1;
  you = n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].v;
    a[i].weizhi = i;
  }
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i + 1][j] =
          max(dp[i + 1][j],
              dp[i][j] + abs(a[i + j + 1].weizhi - n + i) * a[i + j + 1].v);
      if (i + 1 <= n && j <= n)
        ans = max(ans, dp[i + 1][j]);
      dp[i][j + 1] =
          max(dp[i][j + 1],
              dp[i][j] + abs(a[i + j + 1].weizhi - j - 1) * a[i + j + 1].v);
      if (i <= n && j + 1 <= n)
        ans = max(ans, dp[i][j + 1]);
    }
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node {
  int v;
  int weizhi;
};
Node a[2005];
bool cmp(Node a, Node b) {
  if (a.v > b.v)
    return true;
  else if (a.v == b.v && a.weizhi < b.weizhi)
    return true;
  else
    return false;
}
ll dp[2005][2005];
long long ans;
int zuo;
int you;
int main() {
  int n;
  cin >> n;
  zuo = 1;
  you = n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].v;
    a[i].weizhi = i;
  }
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i + 1][j] =
          max(dp[i + 1][j],
              dp[i][j] + abs(a[i + j + 1].weizhi - n + i) * a[i + j + 1].v);
      ans = max(ans, dp[i + 1][j]);
      dp[i][j + 1] =
          max(dp[i][j + 1],
              dp[i][j] + abs(a[i + j + 1].weizhi - j - 1) * a[i + j + 1].v);
      ans = max(ans, dp[i][j + 1]);
    }
  }
  cout << ans;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d", a);
#define pfl(a) printf("%lld", a);
#define pfin(a) printf("%d\\n", a);
#define pfln(a) printf("%lld\\n", a);
#define sfi(a) scanf("%d", &a);
#define sfl(a) scanf("%lld", &a);
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define endl "\\n"
const int MAXN = 2e3 + 5;
const int M = 1e9 + 7;
int n;

ll dp[MAXN][MAXN];
vector<pair<ll, int>> v(MAXN);

ll solve(int i, int j) {
  if (i > j)
    return 0;
  int ind = (n - 1) - (j - i);

  if (dp[i][j] != -1)
    return dp[i][j];

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

  return dp[i][j];
}

int main() {

  sfi(n)

      f(i, 0, n) {
    f(j, 0, n) { dp[i][j] = -1; }
  }

  f(i, 0, n) {
    cin >> v[i].first;
    v[i].second = i;
  }

  sort(v.rbegin(), v.rend());

  ll ans = solve(0, n - 1);

  pfl(ans) return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pfi(a) printf("%d", a);
#define pfl(a) printf("%lld", a);
#define pfin(a) printf("%d\\n", a);
#define pfln(a) printf("%lld\\n", a);
#define sfi(a) scanf("%d", &a);
#define sfl(a) scanf("%lld", &a);
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define endl "\\n"
const int MAXN = 2e3 + 5;
const int M = 1e9 + 7;
int n;

ll dp[MAXN][MAXN];
vector<pair<ll, int>> v(MAXN);

ll solve(int i, int j) {
  // cout<<i<<" "<<j<<endl;
  int ind = (n - 1) - (j - i);

  if (dp[i][j] != -1)
    return dp[i][j];

  dp[i][j] = max(
      i == (n - 1) ? 0
                   : abs(v[ind].second - i) * v[ind].first + solve(i + 1, j),
      j == 0 ? 0 : abs(v[ind].second - j) * v[ind].first + solve(i, j - 1));

  return dp[i][j];
}

int main() {

  sfi(n)

      f(i, 0, n) {
    f(j, 0, n) { dp[i][j] = -1; }
  }

  f(i, 0, n) {
    cin >> v[i].first;
    v[i].second = i;
  }

  sort(v.rbegin(), v.rend());

  ll ans = solve(0, n - 1);

  pfl(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)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

static const int INF = 1001001001;
static const int MAX = 2005;
ll dp[MAX][MAX];

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n + 1) rep(j, n + 1) dp[i][j] = -INF;
  dp[0][0] = 0;
  vector<P> p;
  rep(i, n) p.emplace_back(a[i], i);
  sort(p.rbegin(), p.rend());
  rep(i, n) {
    int pi = p[i].second;
    rep(l, i + 1) {
      int r = i - l;
      dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + ll(pi - l) * a[pi]);
      dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + ll((n - r - 1) - pi) * a[pi]);
    }
  }
  ll ans = 0;
  rep(i, n + 1) ans = max(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

static const int INF = 1001001001;
static const int MAX = 2000;
ll dp[MAX][MAX];

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n + 1) rep(j, n + 1) dp[i][j] = -INF;
  dp[0][0] = 0;
  vector<P> p;
  rep(i, n) p.emplace_back(a[i], i);
  sort(p.rbegin(), p.rend());
  rep(i, n) {
    int pi = p[i].second;
    rep(l, i + 1) {
      int r = i - 1;
      dp[i + 1][l + 1] = max(dp[i][l], dp[i][l] + ll(pi - 1) * a[pi]);
      dp[i + 1][l] = max(dp[i][l], dp[i][l] + ll((n - r - 1) - pi) * a[pi]);
    }
  }
  ll ans = 0;
  rep(i, n + 1) ans = max(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}
Problem p02709 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
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;
}

using pll = pair<long long, long long>;
int main() {
  int N;
  cin >> N;
  vector<pll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i].first, A[i].second = i;
  sort(A.begin(), A.end(), greater<pll>());

  vector<vector<long long>> dp(N + 1, vector<long long>(N + 1, 0));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; i + j < N; ++j) {
      // left
      chmax(dp[i + 1][j], dp[i][j] + A[i + j].first * (A[i + j].second - i));

      // right
      chmax(dp[i][j + 1],
            dp[i][j] + A[i + j].first * ((N - 1 - j) - A[i + j].second));
    }
  }
  long long res = 0;
  for (int i = 0; i <= N; ++i)
    chmax(res, dp[i][N - i]);
  cout << res << endl;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define orep(i, n) for (int i = 1; i <= (n); ++i)     // one rep
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)   // down rep
#define srep(i, s, t) for (int i = (s); i < (t); ++i) // set rep
#define rng(x) (x).begin(), (x).end()                 // range
#define rrng(a) (a).rbegin(), (a).rend()              // reverse range
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define ru(x, y) (((x) + (y)-1) / (y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vl = vector<ll>;

template <typename T> void Yes(T flag) {
  cout << (flag ? "Yes" : "No") << endl;
}

template <typename T> void pv(vector<T> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << v << ",";
  }
  cout << "]" << endl;
}

template <> void pv(vector<P> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << "<" << v.fi << "," << v.se << ">"
         << ",";
  }
  cout << "]" << endl;
}

ll dp[2005][2005];

int main() {
  int n;
  cin >> n;
  vp a(n);
  rep(i, n) {
    int t;
    cin >> t;
    a.at(i) = P(t, i);
  }
  sort(rrng(a));

  rep(i, n) rep(j, n) {
    if (i + j > n)
      continue;
    ll u = a.at(i + j).fi;
    ll d = a.at(i + j).se;
    dp[i][j + 1] = max(dp[i][j] + u * abs(n - d - j - 1), dp[i][j + 1]);
    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + u * abs(d - i));
  }

  ll res = 0;
  rep(i, n + 1) { res = max(res, dp[i][n - i]); }

  cout << res << endl;

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

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

#define fi first
#define se second
const int N = 2010;
int n;
pair<ll, int> a[N];
ll dp[N][N]; // dp[i][j] - prof cu primele i nr , j fiind plasate la stanga

int main() {
  long long ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].fi, a[i].se = i;
  sort(a + 1, a + 1 + n, greater<pair<int, int>>());
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      dp[i][j] = max(dp[i - 1][j] + a[i].fi * abs(n - (i - j) + 1 - a[i].se),
                     dp[i - 1][j - 1] + a[i].fi * abs(a[i].se - j));
    }
    dp[i][i] = dp[i - 1][i - 1] + a[i].fi * abs(a[i].se - i);
    dp[i][0] = dp[i - 1][0] + a[i].fi * abs(n - i + 1 - a[i].se);
  }
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[n][i]);
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

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

#define fi first
#define se second
const int N = 1010;
int n;
pair<int, int> a[N];
ll dp[N][N]; // dp[i][j] - prof cu primele i nr , j fiind plasate la stanga

int main() {
  long long ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].fi, a[i].se = i;
  sort(a + 1, a + 1 + n, greater<pair<int, int>>());
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      dp[i][j] = max(dp[i - 1][j] + a[i].fi * abs(n - (i - j) + 1 - a[i].se),
                     (j > 0 ? dp[i - 1][j - 1] : -(1LL << 60)) +
                         a[i].fi * abs(a[i].se - j));
    }
    dp[i][i] = dp[i - 1][i - 1] + a[i].fi * abs(a[i].se - i);
    dp[i][0] = dp[i - 1][0] + a[i].fi * abs(n - i + 1 - a[i].se);
  }
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[n][i]);
  cout << ans;
  return 0;
}