#include <bits/stdc++.h>
using namespace std;
long long int n, m, k;
long long int a[200005];
void solve() {
  stack<int> s;
  for (int i = 0; i < n; i++) {
    if (s.empty() || s.top() != a[i]) {
      if (!s.empty() && a[i] > s.top()) {
        cout << "NO" << endl;
        exit(0);
      }
      s.push(a[i]);
    } else
      s.pop();
  }
  if (s.size() > 1) {
    cout << "NO" << endl;
    exit(0);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  solve();
  reverse(a, a + n);
  solve();
  cout << "YES" << endl;
  return 0;
}
