#include <bits/stdc++.h>
using namespace std;
const int N = (int)2 * 1e5;
int a[N];
int n;
int main() {
  cin >> n;
  int m = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m = max(m, a[i]);
  }
  bool ok = true;
  vector<int> s;
  for (int i = 0; i < n; i++) {
    if (a[i] == m) {
      if (!s.empty()) {
        ok = false;
        break;
      }
    } else if (!s.empty()) {
      if (s.back() == a[i]) {
        s.pop_back();
      } else if (s.back() < a[i]) {
        ok = false;
        break;
      } else {
        s.push_back(a[i]);
      }
    } else {
      s.push_back(a[i]);
    }
  }
  cout << (ok && s.empty() ? "YES" : "NO") << endl;
}
