#include <bits/stdc++.h>
using namespace std;
int n, x, sum, mx, open;
map<int, int> mp, vis, occ, freq;
deque<int> open_q;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  cin >> n;
  int arr[n + 2];
  for (int i = 1; i <= n; i++) cin >> arr[i], mx = max(mx, arr[i]);
  for (int i = 1; i <= n; i++) {
    if (mx == arr[i]) continue;
    if (vis[arr[i]]) {
      if ((abs(i - vis[arr[i]]) == 1) ||
          (abs(sum - freq[arr[i]]) + 1 == abs(i - vis[arr[i]]))) {
        vis[arr[i]] = 0;
        sum += 2;
        open--;
        open_q.pop_back();
      } else {
        cout << "NO" << endl, exit(0);
      }
    } else {
      if (!open_q.empty() && i > 1 && arr[i] > open_q.back())
        cout << "NO" << endl, exit(0);
      open_q.push_back(arr[i]);
      vis[arr[i]] = i;
      freq[arr[i]] = sum;
      open++;
    }
  }
  if (!open)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
