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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
int MOD = 1000000007;
signed main() {
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  // a[0]={-1,-1};
  rep(i, n) {
    cin >> a[i].first;
    a[i].second = i + 1;
  }
  sort(ALL(a));
  vector<vector<int>> dp(n + 5, vector<int>(n + 5, 0));
  // dp[1][0]=abs(a[n-1].second-1)*a[n-1].first;
  // dp[0][1]=abs(a[n-1].second-n)*a[n-1].first;
  int res = 0;
  rep(i, n + 1) {
    rep(j, n + 1) {
      if (i + j == 0)
        continue;
      if (i == 0) {
        dp[i][j] =
            max(dp[i][j], a[n - (i + j)].first *
                                  (abs(a[n - (i + j)].second - (n - j + 1))) +
                              dp[i][j - 1]);
        continue;
      } else if (j == 0) {
        dp[i][j] = max(dp[i][j],
                       a[n - (i + j)].first * (abs(a[n - (i + j)].second - i)) +
                           dp[i - 1][j]);
        continue;
      }
      dp[i][j] = max(dp[i][j],
                     a[n - (i + j)].first * (abs(a[n - (i + j)].second - i)) +
                         dp[i - 1][j]);
      dp[i][j] =
          max(dp[i][j], a[n - (i + j)].first *
                                (abs(a[n - (i + j)].second - (n - j + 1))) +
                            dp[i][j - 1]);
      if (i + j == n) {
        res = max(res, dp[i][j]);
        break;
      }
    }
  }
  cout << res << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const int inf = 1000000007;
const int MOD = 1000000007;
int n;
signed main() {
  cin >> n;
  vector<P> a(n + 1);
  a[0].first = -1;
  a[0].second = -1;
  rep1(i, n) {
    cin >> a[i].first;
    a[i].second = i;
  }
  int res = 0;
  int ans = 0;
  sort(a.begin(), a.end());
  vector<bool> memo(n + 1, true);
  int left = 1;
  int right = n;
  queue<P> buf;
  rep(j, n) {
    int tmp = 0;
    left = 1;
    right = n;
    res = 0;
    for (int i = n; i > 0; i--) {
      if (a[i].first == inf)
        continue;
      if (a[i].second > j)
        continue;
      if (abs(right - a[i].second) >= abs(a[i].second - left)) {
        res += abs(right - a[i].second) * a[i].first;
        right--;
        a[i].first = inf;
        if (!buf.empty()) {
          res += abs(left - buf.front().second) * buf.front().first;
          buf.pop();
          a[i + 1].first = inf;
          left++;
        }
      }
    }
    for (int i = n; i > 0; i--) {
      if (a[i].first == inf)
        continue;
      // if(a[i].second>j)continue;
      if (abs(right - a[i].second) == abs(a[i].second - left)) {
        buf.push(a[i]);
        continue;
      } else if (abs(right - a[i].second) > abs(a[i].second - left)) {
        res += abs(right - a[i].second) * a[i].first;
        right--;
        if (!buf.empty()) {
          res += abs(left - buf.front().second) * buf.front().first;
          buf.pop();
          left++;
        }
      } else {
        res += abs(left - a[i].second) * a[i].first;
        left++;
        if (!buf.empty()) {
          res += abs(right - buf.front().second) * buf.front().first;
          buf.pop();
          right--;
        }
      }
      // printf("%lld %lld\\n",left,right);
      // cout<<res<<"\\n";
    }
    buf.pop();
    res += abs(left - a[1].second) * a[1].first;
    ans = max(res, ans);
  }
  cout << ans << "\\n";
  return 0;
}