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

#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a < b) ? a : b)
#define max(a, b) ((a < b) ? b : a)
#define pb push_back
#define endl "\\n"
typedef long long ll;

#define max_element mmmmm
#define min_element nnnnn

#define int long long

template <class T> class BIT {
public:
  int BIT_size;
  vector<T> BIT_array;
  BIT(int BIT_size) {
    this->BIT_size = BIT_size;
    BIT_array = vector<T>(BIT_size + 1);
    fill(BIT_array.begin(), BIT_array.end(), 0);
  }
  void BIT_update(int i, T d) {
    for (; i <= BIT_size; i += (i & -i)) {
      BIT_array[i] += d;
    }
  }
  T BIT_sum(int i) {
    T BIT_sum_result = 0;
    for (; i > 0; i -= (i & -i)) {
      BIT_sum_result += BIT_array[i];
    }
    return BIT_sum_result;
  }
};

ll pow(ll a, ll b, ll m) {
  a %= m;
  ll result = 1;
  while (b > 0) {
    if (b & 1) {
      result = result * a % m;
    }
    a = a * a % m;
    b /= 2;
  }
  return result;
}

signed main() {
  int n;
  cin >> n;
  vector<int> values;
  set<int> no_repeat;
  map<int, vector<int>> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    m[x].push_back(i);
    values.push_back(x);
    no_repeat.insert(x);
  }
  sort(values.begin(), values.end());
  reverse(values.begin(), values.end());
  int pos[n];
  for (int i = 0; i < n; i++) {
    pos[i] = m[values[i]][0];
    m[values[i]].erase(m[values[i]].begin());
  }
  int dp[n + 1][n + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++)
      dp[i][j] = 0;
  }
  for (int len = 1; len <= n; len++) {
    for (int i = 0; i <= len; i++) {
      int j = len - i;
      // cout << "ij " << i << " " << j << endl;
      if (i > 0) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j] + abs(pos[i + j - 1] + 1 - i) *
                                                    values[i + j - 1]);
        // cout << "A " << pos[i+j-1]+1-i << " " <<
        // values[i+j-1] << endl;
      }
      if (j > 0) {
        dp[i][j] = max(dp[i][j], dp[i][j - 1] + abs(pos[i + j - 1] - (n - j)) *
                                                    values[i + j - 1]);
        // cout << "B " << pos[i+j-1]+1-j << " " <<
        // values[i+j-1] << endl;
      }
      // cout << dp[i][j] << endl;
    }
  }
  int solution = 0;
  for (int i = 0; i <= n; i++)
    solution = max(solution, dp[i][n - i]);
  cout << solution << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a < b) ? a : b)
#define max(a, b) ((a < b) ? b : a)
#define pb push_back
#define endl "\\n"
typedef long long ll;

#define max_element mmmmm
#define min_element nnnnn

template <class T> class BIT {
public:
  int BIT_size;
  vector<T> BIT_array;
  BIT(int BIT_size) {
    this->BIT_size = BIT_size;
    BIT_array = vector<T>(BIT_size + 1);
    fill(BIT_array.begin(), BIT_array.end(), 0);
  }
  void BIT_update(int i, T d) {
    for (; i <= BIT_size; i += (i & -i)) {
      BIT_array[i] += d;
    }
  }
  T BIT_sum(int i) {
    T BIT_sum_result = 0;
    for (; i > 0; i -= (i & -i)) {
      BIT_sum_result += BIT_array[i];
    }
    return BIT_sum_result;
  }
};

ll pow(ll a, ll b, ll m) {
  a %= m;
  ll result = 1;
  while (b > 0) {
    if (b & 1) {
      result = result * a % m;
    }
    a = a * a % m;
    b /= 2;
  }
  return result;
}

int main() {
  int n;
  cin >> n;
  int v[n + 1];
  map<int, vector<int>> m;
  vector<int> rank;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    rank.push_back(v[i]);
    m[v[i]].push_back(i);
  }
#define map m
  int clock = n;
  int l = 1, r = n;
  ll solution = 0;
  while ((clock--) - 0) {
    // cout << clock << endl;
    int utility = 0, state, di, dj, X;
    // cout << rank.size() << endl;
    for (int i = 0; i < rank.size(); i++) {
      int x = rank[i];
      // cout << x << " " << i << endl;
      int len = map[rank[i]].size();
      for (int j = 0; j < len; j++) {
        // int index;
        if (abs(map[rank[i]][j] - l) > abs(map[rank[i]][j] - r)) {
          if (x * abs(map[rank[i]][j] - l) > utility) {
            state = 0;
            utility = x * abs(map[rank[i]][j] - l);
            di = i;
            dj = j;
            X = x;
          }
        } else {
          if (x * abs(map[rank[i]][j] - r) > utility) {
            state = 1;
            utility = x * abs(map[rank[i]][j] - r);
            di = i;
            dj = j;
            X = x;
          }
        }
      }
    }
    solution += utility;
    if (clock == 0)
      continue;
    // cout << state << " " << X << " " << dj << " " << di << endl;
    if (state == 0)
      l++;
    if (state == 1)
      r--;
    m[X].erase(m[X].begin() + dj);
    rank.erase(rank.begin() + di);
  }
  cout << solution << endl;
}