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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  int64_t N;
  cin >> N;
  vector<pair<int64_t, int64_t>> A(N);
  for (int64_t i = 0; i < N; ++i) {
    int64_t A_sub;
    cin >> A_sub;
    A[i] = make_pair(A_sub, i);
    // cin >> A.at(i).first;
    // A.at(i).second = i;
  }
  // sort(A.begin(),A.end(),greater<int64_t>());
  sort(A.begin(), A.end());

  /*for ( int i = 0; i < N; ++i){
      cout << A.at(i).first << A.at(i).second << endl;
  }*/

  vector<vector<int64_t>> DP(N + 1, vector<int64_t>(N + 1));
  // DP[x][y] 活発度の高い方から x+y 人を配置
  // x 人を右、y 人を左

  int64_t res = 0;
  for (int64_t i = 0; i <= N; ++i) {
    for (int64_t j = 0; j <= N - i; ++j) {
      if (i == 0 and j == 0) {
        DP.at(i).at(j) = 0;
        continue;
      }
      int64_t num = i + j;
      int64_t Act = A.at(N - num).first;
      int64_t pos = A.at(N - num).second;
      // int64_t right = abs(N - 1 - pos - i);
      // int64_t left = abs(pos - j);
      // cout << Act << pos << right << left << endl;

      if (i == 0) {
        // DP.at(i).at(j) = DP.at(i).at(j-1) + Act * right;
        // DP.at(i).at(j) = DP.at(i).at(j-1) + Act * abs( N - 1 - pos - (i-1) );
        DP.at(i).at(j) = DP.at(i).at(j - 1) + Act * abs(pos - (j - 1));
      } else if (j == 0) {
        // DP.at(i).at(j) = DP.at(i-1).at(j) + Act * left;
        // DP.at(i).at(j) = DP.at(i-1).at(j) + Act * abs(pos - (j-1) );
        DP.at(i).at(j) = DP.at(i - 1).at(j) + Act * abs(N - 1 - pos - (i - 1));
      } else {
        // DP.at(i).at(j) = max(DP.at(i).at(j-1) + Act * right, DP.at(i-1).at(j)
        // + Act * left);
        DP.at(i).at(j) =
            max(DP.at(i - 1).at(j) + Act * abs(N - 1 - pos - (i - 1)),
                DP.at(i).at(j - 1) + Act * abs(pos - (j - 1)));
      }
      // cout << i << " " <<  j << " " << DP.at(i).at(j) << endl;;
      if (num == N) {
        res = max(res, DP.at(i).at(j));
        break;
      }
    }
  }

  cout << res << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
  }

  vector<pair<int, int>> B(N);
  // 嬉しさ、場所
  for (int i = 0; i < N; ++i) {
    B[i] = make_pair(A.at(i), i);
  }
  sort(B.begin(), B.end());

  /*for ( int i = 0; i < N; ++i){
      cout << B.at(i).first << B.at(i).second << endl;
  }*/

  int menL = 0, menR = 0;
  int lngL = 0, lngR = 0;
  int res = 0;

  for (int i = N - 1; i >= 0; --i) {
    int happy = B.at(i).first;
    int pos = B.at(i).second;
    char W;

    lngL = pos - menL;
    lngR = N - 1 - pos - menR;
    if (lngL > lngR) {
      ++menL;
      res = res + lngL * happy;
      W = 'L';
    } else if (lngL < lngR) {
      ++menR;
      res = res + lngR * happy;
      W = 'R';
    } else {
      int next_pos = B.at(i - 1).second;
      if (next_pos > pos) {
        ++menR;
        res = res + lngR * happy;
        W = 'R';
      } else {
        ++menL;
        res = res + lngL * happy;
        W = 'L';
      }
    }
    // cout << W << res << endl;
  }

  cout << res << endl;
  return 0;
}