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

#include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  rep(i, n) {
    ll aa;
    cin >> aa;
    a[i] = make_pair(aa, i);
  }
  sort(a.rbegin(), a.rend());
  VV dp(n + 1, V(n + 1, -1e9));
  dp[0][0] = 0;
  rep(i, n) {
    dp[i + 1][0] = dp[i][0] + a[i].first * (a[i].second - i);
    dp[0][i + 1] = dp[0][i] + a[i].first * (n - 1 - i - a[i].second);
  }
  for (ll sum = 1; sum <= n; sum++) {
    for (ll i = 1; i < sum; i++) {
      dp[i][sum - i] = max(dp[i][sum - i],
                           dp[i - 1][sum - i] +
                               a[sum - 1].first * (a[sum - 1].second - i + 1));
      dp[i][sum - i] =
          max(dp[i][sum - i],
              dp[i][sum - i - 1] +
                  a[sum - 1].first * (n - sum + i - a[sum - 1].second));
    }
  }
  ll ans = 0;
  rep(i, n + 1) { ans = max(ans, dp[i][n - i]); }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
using namespace std;

int main() {
  ll n;
  cin >> n;
  V a(n);
  rep(i, n) cin >> a[i];
  vector<bool> used(n, false);
  ll l = 0;
  ll r = n - 1;
  V ans(n, -1);
  ll ansnum = 0;
  rep(i, n) {
    ll num = -1;
    ll pos = -1;
    rep(j, n) {
      if (!used[j]) {
        if (num < a[j]) {
          num = a[j];
          pos = j;
        }
      }
    }
    used[pos] = true;
    if (abs(l - pos) > abs(r - pos)) {
      ll ok = ansnum + abs(l - pos) * a[pos];
      ll tmp = ansnum;
      V tmpans = ans;
      ll tmpr = r;
      bool aaa = false;
      V tttans;
      ll tmppos = l;
      ll zurakazu = 0;
      for (ll j = l - 1; j >= 0; j--) {
        ansnum += a[ans[j]] * (abs(tmpr - ans[j]) - abs(j - ans[j]));
        tmpans[j] = -1;
        tmpans[tmpr] = ans[j];
        if (abs(j - pos) * a[pos] + ansnum >= ok) {
          ok = abs(j - pos) * a[pos] + ansnum;
          aaa = true;
          tttans = tmpans;
          tmppos = j;
          zurakazu = l - 1 - j + 1;
        }
        tmpr--;
      }
      if (aaa) {
        tttans[tmppos] = pos;
        ans = tttans;
        l = tmppos + 1;
        r -= zurakazu;
      } else {
        ans[l] = pos;
        l++;
      }
      ansnum = ok;
    } else {
      ll ok = ansnum + abs(r - pos) * a[pos];
      ll tmp = ansnum;
      V tmpans = ans;
      ll tmpl = l;
      bool aaa = false;
      V tttans;
      ll tmppos = r;
      ll zurakazu = 0;
      for (ll j = r + 1; j < n; j++) {
        ansnum += a[ans[j]] * (abs(tmpl - ans[j]) - abs(j - ans[j]));
        tmpans[j] = -1;
        tmpans[tmpl] = ans[j];
        if (abs(j - pos) * a[pos] + ansnum >= ok) {
          ok = abs(j - pos) * a[pos] + ansnum;
          aaa = true;
          tttans = tmpans;
          tmppos = j;
          zurakazu = r + 1 - j + 1;
        }
        tmpl--;
      }
      if (aaa) {
        tttans[tmppos] = pos;
        ans = tttans;
        r = tmppos - 1;
        l += zurakazu;
      } else {
        ans[r] = pos;
        r--;
      }
      ansnum = ok;
    }
  }
  cout << ansnum << endl;
}