#include <bits/stdc++.h>
using namespace std;
int main() {
  deque<int> a;
  int n, max = -1, f = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    if (a.size() && temp == a.back()) {
      if (max < temp) max = temp;
      a.pop_back();
    } else if (a.size() && a.back() > temp) {
      a.push_back(temp);
    } else if (!a.size())
      a.push_back(temp);
    else {
      f++;
      break;
    }
  }
  if (!f) {
    if (a.size() == 0)
      cout << "YES" << endl;
    else if (a.size() == 1 && max <= a.back())
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else
    cout << "NO" << endl;
  return 0;
}
