#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 7;
string str[200];
vector<vector<pair<string, int> > > v;
map<pair<string, int>, int> m;
int a[MAXN];
int n;
int main() {
  while (cin >> n) {
    int Max = 0;
    for (int i = 1; i <= n; i++) cin >> a[i], Max = max(a[i], Max);
    stack<int> s;
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      if (s.empty())
        s.push(a[i]);
      else {
        if (s.top() == a[i]) {
          while (!s.empty() && a[i] == s.top()) s.pop();
        } else {
          if (s.empty())
            s.push(a[i]);
          else {
            if (s.top() > a[i]) {
              s.push(a[i]);
            } else {
              cout << "No" << endl;
              return 0;
            }
          }
        }
      }
    }
    if (s.size() == 0) {
      cout << "YES" << endl;
    } else if (s.size() == 1) {
      if (s.top() >= Max)
        cout << "YES" << endl;
      else {
        cout << "NO" << endl;
      }
    } else
      cout << "NO" << endl;
  }
  return 0;
}
