#include <bits/stdc++.h>
using namespace std;
long long n, high = 0, top;
long long a[200005], stackh[200005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (top == 0 || a[i] != stackh[top]) {
      stackh[++top] = a[i];
    } else {
      if (top == 1 || a[i] < stackh[top - 1]) {
        top--;
      }
    }
    if (a[i] > high) {
      high = a[i];
    }
  }
  if (top > 1) {
    cout << "NO" << endl;
  } else if (top == 1) {
    if (stackh[top] == high) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  } else if (top == 0) {
    cout << "YES" << endl;
  }
  return 0;
}
