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

// https://twitter.com/armeria_betrue/status/1251868789773238273
// を見て実装

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

int main(void) {
  cin.sync_with_stdio(false);
  int N;
  cin >> N;
  vector<ll> As(N);
  REP(n, N) cin >> As[n];

  // <val, index>
  vector<pair<ll, int>> Bs(N);
  REP(n, N) { Bs[n] = mp(As[n], n); }
  sort(Bs.rbegin(), Bs.rend());

  // 大きい要素から順番に、右または左に寄せる
  // dp[何番目まで見たか][左に寄せたのは何個か]
  vector<vector<ll>> dp(N + 1, vector<ll>(N + 1));

  REP(n, N) {
    auto a = Bs[n].first;
    auto idx = Bs[n].second;

    REP(left_num, n + 1) {
      // この時点で左にleft_num, 右にright_num存在
      int right_num = n - left_num;
      // aを左に寄せる場合
      chmax(dp[n + 1][left_num + 1], dp[n][left_num] + a * abs(idx - left_num));
      // aを右に寄せる場合
      auto idx2 = N - 1 - right_num;
      chmax(dp[n + 1][left_num], dp[n][left_num] + a * abs(idx - idx2));
    }
  }

  ll ans = 0;
  FOR(i, 1, N + 1) chmax(ans, dp[N][i]);
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// https://twitter.com/armeria_betrue/status/1251868789773238273
// を見て実装

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

int main(void) {
  cin.sync_with_stdio(false);
  int N;
  cin >> N;
  vector<ll> As(N);
  REP(n, N) cin >> As[n];

  // <val, index>
  vector<pair<ll, int>> Bs(N);
  REP(n, N) { Bs[n] = mp(As[n], n); }
  sort(Bs.rbegin(), Bs.rend());

  // 大きい要素から順番に、右または左に寄せる
  // dp[何番目まで見たか][左に寄せたのは何個か]
  vector<vector<ll>> dp(N + 1, vector<ll>(N));

  REP(n, N) {
    auto a = Bs[n].first;
    auto idx = Bs[n].second;

    REP(left_num, n + 1) {
      // この時点で左にleft_num, 右にright_num存在
      int right_num = n - left_num;
      // aを左に寄せる場合
      chmax(dp[n + 1][left_num + 1], dp[n][left_num] + a * abs(idx - left_num));
      // aを右に寄せる場合
      auto idx2 = N - 1 - right_num;
      chmax(dp[n + 1][left_num], dp[n][left_num] + a * abs(idx - idx2));
    }
  }

  ll ans = 0;
  FOR(i, 1, N + 1) chmax(ans, dp[N][i]);
  cout << ans << endl;

  return 0;
}