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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 5100;
const int MOD = 1000000007;

vector<vector<ll>> dp(MAX, vector<ll>(MAX, -INF));

int main() {
  ll N;
  cin >> N;
  vector<P> a(N);
  rep(i, N) {
    cin >> a[i].first;
    a[i].second = i + 1;
  }
  sort(a.begin(), a.end(), greater<P>());

  // vector<vector<ll> > dp(N+1, vector<ll>(N+1, -INF));

  dp[0][0] = 0;

  // dp[i][j]は左にi,みぎにj移動が決まった場合の最大値
  rep(i, N + 1) {
    rep(j, N + 1) {
      if (i == 0) {
        dp[i][j + 1] = dp[i][j] + a[j].first * abs(a[j].second - (N - j));
      } else {
        if (j == 0) {
          dp[i][j] = dp[i - 1][j] + a[i - 1].first * abs(a[i - 1].second - i);
        } else {
          // 左側or右側 a[i+j-1]の追加 左側はすでにi-1こ、右側はすでにj-1こ
          dp[i][j] = max(
              dp[i - 1][j] + a[i + j - 1].first * abs(a[i + j - 1].second - i),
              dp[i][j - 1] + a[i + j - 1].first *
                                 abs(a[i + j - 1].second - (N - (j - 1))));
        }
      }
    }
  }

  // check
  /*
  rep(i,N+1){
    rep(j,N+1){
      cout<<dp[i][j]<<" ";
    }
    cout<<endl;
  }
  */

  ll ans = 0;
  rep(i, N + 1) {
    // cout<<dp[i][N-i]<<endl;
    ans = max(ans, dp[i][N - i]);
  }
  cout << ans << endl;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<P> a(N);
  rep(i, N) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.begin(), a.end(), greater<P>());

  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1, -INF));

  dp[0][0] = 0;

  // dp[i][j]は左にi,みぎにj移動が決まった場合の最大値
  rep(i, N + 1) {
    rep(j, N + 1) {
      if (i == 0) {
        dp[i][j + 1] = dp[i][j] + a[j].first * (abs((N - 1 - j) - a[j].second));
      } else {
        if (j == 0) {
          dp[i][j] =
              dp[i - 1][j] + a[i - 1].first * (abs(a[i - 1].second - (i - 1)));
        } else {
          if (i + j > N + 1)
            continue;
          // どっちかからのmaxをとる
          dp[i][j] =
              max(dp[i - 1][j] +
                      a[i + j - 1].first * (abs(a[i + j - 1].second - (i - 1))),
                  dp[i][j - 1] + a[i + j - 1].first * (abs(a[i + j - 1].second -
                                                           (N - 1 - (j - 1)))));
          // 右からN-1 - j
        }
      }
    }
  }

  // check
  /*
  rep(i,N+1){
    rep(j,N+1){
      cout<<dp[i][j]<<" ";
    }
    cout<<endl;
  }
  */

  ll ans = 0;
  rep(i, N + 1) { ans = max(ans, dp[i][N - i]); }
  cout << ans << endl;
}