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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int, int>>
#define IO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define IOfiles                                                                \\
  freopen("input.txt", "r+", stdin);                                           \\
  freopen("output.txt", "w+", stdout)
#define PI 3.14159265
const long long mod = 1e9 + 7LL;
typedef long long ll;
typedef pair<int, int> ii;
typedef double db;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll pwr(ll x, ll n) {
  ll ans = 1;
  while (n) {
    ans *= x;
    n--;
  }
  return ans;
}

void solve() {}

int main() {
  IO;
  ll n;
  cin >> n;
  ll dp[n][n];
  memset(dp, -1, sizeof dp);
  vector<pair<ll, ll>> v;
  for (int i = 0; i < n; i++) {
    ll val;
    cin >> val;
    v.pb(make_pair(val, i));
  }
  sort(all(v));
  dp[0][n - 1] = 0;
  ll ans = 0;

  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i; j--) {
      int p = j - i;
      if (p == 0) {
        dp[i][j] += v[0].fi * abs(v[0].se - i);
        ans = max(ans, dp[i][j]);
        continue;
      }
      /*cout<<i<<" "<<j<<" ";
      cout<<p<<endl;*/
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + v[p].fi * abs(v[p].se - i));
      dp[i][j - 1] = max(dp[i][j - 1], dp[i][j] + v[p].fi * abs(v[p].se - j));
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int, int>>
#define IO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define IOfiles                                                                \\
  freopen("input.txt", "r+", stdin);                                           \\
  freopen("output.txt", "w+", stdout)
#define PI 3.14159265
const long long mod = 1e9 + 7LL;
typedef long long ll;
typedef pair<int, int> ii;
typedef double db;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll pwr(ll x, ll n) {
  ll ans = 1;
  while (n) {
    ans *= x;
    n--;
  }
  return ans;
}

void solve() {}

int main() {
  IO;
  ll n;
  cin >> n;
  ll dp[n + 1][n][n];
  memset(dp, -1, sizeof dp);
  vector<pair<ll, ll>> v;
  for (int i = 0; i < n; i++) {
    ll val;
    cin >> val;
    v.pb(make_pair(val, i));
  }
  sort(all(v));
  dp[n][0][n - 1] = 0;
  for (int p = n; p > 0; p--) {
    for (int i = 0; i < n; i++) {
      for (int j = n - 1; j >= 0; j--) {
        if (dp[p][i][j] != -1) {
          dp[p - 1][i + 1][j] =
              max(dp[p - 1][i + 1][j],
                  dp[p][i][j] + v[p - 1].fi * abs(v[p - 1].se - i));
          dp[p - 1][i][j - 1] =
              max(dp[p - 1][i][j - 1],
                  dp[p][i][j] + v[p - 1].fi * abs(v[p - 1].se - j));
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      ans = max(ans, dp[0][i][j]);
  /*for(int p=n;p>=0;p--){
      cout<<"layer: "<<p<<endl;
      for(int i=0;i<n;i++){
          for(int j=0;j<n;j++){
              cout<<dp[p][i][j]<<" ";
          }
          cout<<endl;
      }
  }*/
  cout << ans << endl;
}