Problem p02709 (C++) — buggy submission, verdict: Runtime Error

//
// Created by yamunaku on 2020/04/19.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define CST(x) cout << fixed << setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template <typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

int main() {
  // CFS;
  int n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  vi idx(n);
  iota(all(idx), 0);
  sort(all(idx), [&](int &l, int &r) { return a[l] > a[r]; });
  vl dp(n + 1, 0);
  rep(i, n) {
    vl ndp(n, 0);
    rep(j, i + 1) {
      if (j + 1 < n)
        ndp[j + 1] = max(ndp[j + 1], dp[j] + a[idx[i]] * abs(j - idx[i]));
      ndp[j] = max(ndp[j], dp[j] + a[idx[i]] * abs(n - 1 - (i - j) - idx[i]));
    }
    dp = ndp;
  }
  ll ans = 0;
  rep(i, n + 1) { ans = max(ans, dp[i]); }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

//
// Created by yamunaku on 2020/04/19.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define CST(x) cout << fixed << setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template <typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

int main() {
  // CFS;
  int n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  vector<pair<int, int>> p(n);
  rep(i, n) { p[i] = {a[i], i}; }
  sort(all(p),
       [&](pair<int, int> &l, pair<int, int> &r) { return l.first > r.first; });
  vl dp(n + 1, 0);
  rep(i, n) {
    vl ndp(n, 0);
    rep(j, i + 1) {
      ndp[j + 1] = max(ndp[j + 1], dp[j] + p[i].first * abs(j - p[i].second));
      ndp[j] =
          max(ndp[j], dp[j] + p[i].first * abs(n - 1 - (i - j) - p[i].second));
    }
    dp = ndp;
  }
  ll ans = 0;
  rep(i, n + 1) { ans = max(ans, dp[i]); }
  cout << ans << endl;
  return 0;
}