#include <bits/stdc++.h>
using namespace std;
long long n, i = 0, x, cnt[2] = {0}, y;
vector<long long> v;
stack<int> s;
int main(void) {
  cin >> n;
  for (i = 0; i < n; i++) cin >> x, v.push_back(x);
  y = *max_element(v.begin(), v.end());
  for (i = 0; i < n; i++) {
    if (!s.empty() && s.top() == (v[i])) {
      s.pop();
    } else if (s.empty())
      s.push(v[i]);
    else if (v[i] > s.top())
      return cout << "NO", 0;
    else
      s.push(v[i]);
  }
  if (s.size() == 0 || (s.size() == 1 && s.top() == y))
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
