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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false

signed main(void) {
  int N;
  const int MEM = 2020;
  int A[MEM];

  cin >> N;
  REP(i, N) { cin >> A[i]; }

  vector<pair<int, int>> P;
  vector<int> Q(N, 0);
  REP(i, N) { P.push_back({A[i], i}); }

  sort(P.begin(), P.end(), greater<>());

  int dp[MEM][MEM] = {};
  REP(i, MEM) {
    REP(j, MEM) { dp[i][j] = -INF; }
  }
  dp[0][0] = 0;

  REP(j, N) {
    REP(i, j + 1) {
      int k = j - i;
      // 左
      dp[i + 1][k] =
          max(dp[i + 1][k], dp[i][k] + P[j].first * (abs(i - P[j].second)));
      // 右
      dp[i][k + 1] = max(
          dp[i][k + 1], dp[i][k] + P[j].first * (abs(N - 1 - k - P[j].second)));
    }
  }

  int Ans = 0;
  REP(i, N + 1) { Ans = max(Ans, dp[i][N - i]); }
  cout << Ans << endl;

#if debug
  REP(i, N + 1) {
    REP(j, N + 1) {
      if (dp[i][j] < -INF / 10) {
        cout << "-∞"
             << " ";
      } else {
        cout << dp[i][j] << " ";
      }
    }
    cout << endl;
  }
#endif

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false

signed main(void) {
  int N;
  const int MEM = 60;
  int A[MEM];

  cin >> N;
  REP(i, N) { cin >> A[i]; }

  vector<pair<int, int>> P;
  vector<int> Q(N, 0);
  REP(i, N) { P.push_back({A[i], i}); }

  sort(P.begin(), P.end(), greater<>());

  int dp[MEM][MEM] = {};
  REP(i, MEM) {
    REP(j, MEM) { dp[i][j] = -INF; }
  }
  dp[0][0] = 0;

  REP(j, N) {
    REP(i, j + 1) {
      int k = j - i;
      // 左
      dp[i + 1][k] =
          max(dp[i + 1][k], dp[i][k] + P[j].first * (abs(i - P[j].second)));
      // 右
      dp[i][k + 1] = max(
          dp[i][k + 1], dp[i][k] + P[j].first * (abs(N - 1 - k - P[j].second)));
    }
  }

  int Ans = 0;
  REP(i, N + 1) { Ans = max(Ans, dp[i][N - i]); }
  cout << Ans << endl;

#if debug
  REP(i, N + 1) {
    REP(j, N + 1) {
      if (dp[i][j] < -INF / 10) {
        cout << "-∞"
             << " ";
      } else {
        cout << dp[i][j] << " ";
      }
    }
    cout << endl;
  }
#endif

  return 0;
}