#include <bits/stdc++.h>
using namespace std;
vector<int> A, P;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ((void)0);
  ((void)0);
  ((void)0);
  int N, M = 0;
  cin >> N;
  A.resize(N);
  P.resize(N, -1);
  for (auto &a : A) {
    cin >> a;
    M = max(M, a);
  }
  for (int i = 1; i < N; i++) {
    if (A[i] == M) continue;
    if (P[i - 1] == -1) {
      if (A[i - 1] == A[i]) {
        P[i - 1] = i;
        P[i] = i - 1;
      }
    } else {
      int p = i - 1;
      while (p >= 0 && P[p] != -1) {
        if (A[p] <= A[i]) {
          p = P[p] - 1;
        } else {
          p = -1;
        }
      }
      if (p != -1 && A[p] == A[i]) {
        P[p] = i;
        P[i] = p;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (A[i] != M && P[i] == -1) {
      cout << "NO\n";
      return 0;
    }
  }
  cout << "YES\n";
  return 0;
}
