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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define double long double
#define itn int
#define endl '\\n'
#define co(ans) cout << ans << endl;
#define COYE cout << "YES" << endl;
#define COYe cout << "Yes" << endl;
#define COye cout << "yes" << endl;
#define CONO cout << "NO" << endl;
#define CONo cout << "No" << endl;
#define COno cout << "no" << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define ALL(V) ((V).begin(), (V).end())
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  ll N, ans = 0ll, cnt = 0ll;
  cin >> N;
  ll A[N], DP[N + 1][N + 1];
  REP(i, N) cin >> A[i];
  REP(i, N + 1) {
    REP(j, N + 1) { DP[i][j] = 0; }
  }

  vector<pair<ll, ll>> P;
  REP(i, N) P.push_back(make_pair(A[i], i));
  SORT(P);
  REVERSE(P);

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= i; ++j) {
      DP[j + 1][i - j] = max(DP[j + 1][i - j],
                             DP[j][i - j] + P[i].first * abs(P[i].second - j));
      DP[j][i - j + 1] =
          max(DP[j][i - j + 1],
              DP[j][i - j] + P[i].first * abs(P[i].second - N + i - j + 1));
    }
  }

  for (int i = 0; i <= N; ++i) {
    if (ans < DP[i][N - i])
      ans = DP[i][N - i];
  }

  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define double long double
#define itn int
#define endl '\\n'
#define co(ans) cout << ans << endl;
#define COYE cout << "YES" << endl;
#define COYe cout << "Yes" << endl;
#define COye cout << "yes" << endl;
#define CONO cout << "NO" << endl;
#define CONo cout << "No" << endl;
#define COno cout << "no" << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define ALL(V) ((V).begin(), (V).end())
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  ll N, ans = 0ll, cnt = 0ll;
  cin >> N;
  ll A[N];
  REP(i, N) cin >> A[i];

  for (ll i = 0ll; i <= N; ++i) {
    cnt = 0ll;

    vector<pair<ll, ll>> P, Q;
    for (ll j = 0; j < i; ++j)
      P.push_back(make_pair(A[j], j));
    for (ll j = i; j < N; ++j)
      Q.push_back(make_pair(A[j], j));

    SORT(P);
    SORT(Q);
    REVERSE(Q);

    for (ll j = 0ll; j < N - i; ++j) {
      cnt += Q[j].first * abs(Q[j].second - j);
    }

    for (ll j = 0ll; j < i; ++j) {
      cnt += P[j].first * abs(N - i + j - P[j].second);
    }

    SORT(Q);
    if (P[0].first == Q[0].first) {
      ll T = P[0].first, cnta = 0, cntb = 0, kk;

      for (ll j = 0; j < P.size(); ++j) {
        if (P[j].first != T) {
          cnta = j;
          break;
        }
      }

      for (ll j = 0; j < Q.size(); ++j) {
        if (Q[j].first != T) {
          cntb = j;
          break;
        }
      }

      if (cnta < cntb)
        kk = cnta;
      else
        kk = cntb;

      cnt += 2 * kk * kk;
    }

    if (cnt > ans)
      ans = cnt;
  }

  cout << ans << endl;
  return 0;
}