Problem p02709 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

// definitions
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define rep(i, l, r) for (long long i = (l); i < (long long)(r); i++)
#define repb(i, r, l) for (long long i = (r); i > (long long)(l); i--)
#define pb push_back
#define mod1 998244353
#define mod 1000000007
#define MAX_CHAR 256

using namespace std;
typedef vector<long long> vl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pair<long long, long long>> vp;
typedef vector<tuple<long long, long long, long long>> vtup;
typedef deque<long long> dq;
typedef long long int lli;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

// standard functions
ll gcd(ll x, ll y) {
  if (x < y)
    return gcd(y, x);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

ll modexp(ll x, ll ex, ll modp) {
  ll ans = 1ll;
  while (ex > 0) {
    if (ex & 1ll)
      ans = (ans * x) % modp;
    ex >>= 1ll;
    x = (x * x) % modp;
  }
  return ans;
}

ll nCrModp(ll n, ll r, ll p) {
  ll C[r + 1];
  memset(C, 0, sizeof(C));

  C[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, r); j > 0; j--)
      C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}

bool isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

vl countFreq(ll arr[], ll n) {
  unordered_map<ll, ll> mp;
  vector<ll> v;

  for (ll i = 0; i < n; i++)
    mp[arr[i]]++;

  for (auto x : mp)
    v.pb(x.second);

  return v;
}

ll binarySearch(ll arr[], ll l, ll r, ll x) {
  while (l <= r) {
    ll m = l + (r - l) / 2;
    if (arr[m] == x)
      return m;
    if (arr[m] < x)
      l = m + 1;
    else
      r = m - 1;
  }
  return -1;
}

const ll MAXSIEVE = (1e3);
ll spf[MAXSIEVE];
void sieve() {
  spf[1] = 1;
  for (ll i = 2; i < MAXSIEVE; i++)
    spf[i] = i;

  for (ll i = 4; i < MAXSIEVE; i += 2)
    spf[i] = 2;

  for (ll i = 3; i * i < MAXSIEVE; i++) {
    if (spf[i] == i) {
      for (ll j = i * i; j < MAXSIEVE; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// code beigns in main
int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin >> t;

  while (t--) {
    ll n;
    cin >> n;

    vp v;
    rep(i, 0, n) {
      ll val;
      cin >> val;
      v.pb({val, i + 1});
    }

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

    ll dp[n + 1][n + 1];
    clr(dp);

    rep(i, 1, n + 1) {
      repb(j, i, 0) {
        ll cnt = j - i + n - 1;
        dp[j][i] = max(dp[j + 1][i] + v[cnt].F * abs(v[cnt].S - j),
                       dp[j][i - 1] + v[cnt].F * abs(v[cnt].S - i));
      }
    }

    cout << dp[1][n] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

// definitions
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define rep(i, l, r) for (long long i = (l); i < (long long)(r); i++)
#define repb(i, r, l) for (long long i = (r); i > (long long)(l); i--)
#define pb push_back
#define mod1 998244353
#define mod 1000000007
#define MAX_CHAR 256

using namespace std;
typedef vector<long long> vl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pair<long long, long long>> vp;
typedef vector<tuple<long long, long long, long long>> vtup;
typedef deque<long long> dq;
typedef long long int lli;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

// standard functions
ll gcd(ll x, ll y) {
  if (x < y)
    return gcd(y, x);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

ll modexp(ll x, ll ex, ll modp) {
  ll ans = 1ll;
  while (ex > 0) {
    if (ex & 1ll)
      ans = (ans * x) % modp;
    ex >>= 1ll;
    x = (x * x) % modp;
  }
  return ans;
}

ll nCrModp(ll n, ll r, ll p) {
  ll C[r + 1];
  memset(C, 0, sizeof(C));

  C[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, r); j > 0; j--)
      C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}

bool isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

vl countFreq(ll arr[], ll n) {
  unordered_map<ll, ll> mp;
  vector<ll> v;

  for (ll i = 0; i < n; i++)
    mp[arr[i]]++;

  for (auto x : mp)
    v.pb(x.second);

  return v;
}

ll binarySearch(ll arr[], ll l, ll r, ll x) {
  while (l <= r) {
    ll m = l + (r - l) / 2;
    if (arr[m] == x)
      return m;
    if (arr[m] < x)
      l = m + 1;
    else
      r = m - 1;
  }
  return -1;
}

const ll MAXSIEVE = (1e3);
ll spf[MAXSIEVE];
void sieve() {
  spf[1] = 1;
  for (ll i = 2; i < MAXSIEVE; i++)
    spf[i] = i;

  for (ll i = 4; i < MAXSIEVE; i += 2)
    spf[i] = 2;

  for (ll i = 3; i * i < MAXSIEVE; i++) {
    if (spf[i] == i) {
      for (ll j = i * i; j < MAXSIEVE; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// code beigns in main
int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin >> t;

  while (t--) {
    ll n;
    cin >> n;

    vp v;
    rep(i, 0, n) {
      ll val;
      cin >> val;
      v.pb({val, i + 1});
    }

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

    ll dp[n + 2][n + 2];
    clr(dp);

    rep(i, 1, n + 1) {
      repb(j, i, 0) {
        ll cnt = j - i + n - 1;
        dp[j][i] = max(dp[j + 1][i] + v[cnt].F * abs(v[cnt].S - j),
                       dp[j][i - 1] + v[cnt].F * abs(v[cnt].S - i));
      }
    }

    cout << dp[1][n] << endl;
  }
}