Problem p02919 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v)                                                               \
  {                                                                            \
    for (auto x : v)                                                           \
      cout << x << ' ';                                                        \
    cout << endl;                                                              \
  }
#define printn(v, n)                                                           \
  {                                                                            \
    for (int _i = 0; _i < n; _i++)                                             \
      cout << *(v + _i) << ' ';                                                \
    cout << endl;                                                              \
  }

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 1e3 + 20;
long long n;
long long a[MAXN], pos[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  long long l = min(pos[n], pos[n - 1]), r = max(pos[n], pos[n - 1]);
  set<long long> st({0, l, r, n + 1});
  long long res = (n - 1) * (n - r + 1) * l;
  // cout << res << endl;
  for (int i = n - 2; i >= 1; i--) {
    st.insert(pos[i]);
    auto it1 = st.find(pos[i]);
    it1--;
    auto it2 = it1;
    if (it2 != st.begin())
      it2--;
    auto it3 = st.find(pos[i]);
    it3++;
    auto it4 = it3;
    it4++;
    if (it4 == st.end())
      it4--;

    res += (*it3 - pos[i]) * (*it1 - *it2) * i;
    res += (pos[i] - *it1) * (*it4 - *it3) * i;
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v)                                                               \
  {                                                                            \
    for (auto x : v)                                                           \
      cout << x << ' ';                                                        \
    cout << endl;                                                              \
  }
#define printn(v, n)                                                           \
  {                                                                            \
    for (int _i = 0; _i < n; _i++)                                             \
      cout << *(v + _i) << ' ';                                                \
    cout << endl;                                                              \
  }

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 1e5 + 20;
long long n;
long long a[MAXN], pos[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  long long l = min(pos[n], pos[n - 1]), r = max(pos[n], pos[n - 1]);
  set<long long> st({0, l, r, n + 1});
  long long res = (n - 1) * (n - r + 1) * l;
  // cout << res << endl;
  for (int i = n - 2; i >= 1; i--) {
    st.insert(pos[i]);
    auto it1 = st.find(pos[i]);
    it1--;
    auto it2 = it1;
    if (it2 != st.begin())
      it2--;
    auto it3 = st.find(pos[i]);
    it3++;
    auto it4 = it3;
    it4++;
    if (it4 == st.end())
      it4--;

    res += (*it3 - pos[i]) * (*it1 - *it2) * i;
    res += (pos[i] - *it1) * (*it4 - *it3) * i;
  }
  cout << res << endl;

  return 0;
}