#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t, t1 = -1, t2 = -1;
  stack<int> q;
  cin >> n >> t;
  q.push(t);
  for (int i = 1; i < n; i++) {
    int t;
    cin >> t;
    if (i <= n - 2)
      t1 = max(t1, t);
    else if (i == n - 1)
      t2 = t;
    if (q.size() >= 1 && q.top() == t) {
      if (q.size() == 1)
        q.pop();
      else {
        q.pop();
        if (q.top() < t) {
          q.push(t);
          q.push(t);
        }
      }
    } else
      q.push(t);
  }
  if (q.size() < 1 ||
      (q.size() == 1 && (t1 == t2 || t2 > t1 || n == 1 || (t1 == q.top()))))
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
