#include <bits/stdc++.h>
using namespace std;
int n, s[(int(2e5) + 10)], a[(int(2e5) + 10)], x, cnt = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (cnt == 0 || a[i] != s[cnt]) {
      if (cnt && a[i] > s[cnt]) {
        cout << "NO" << endl;
        return 0;
      }
      s[++cnt] = a[i];
    } else
      cnt--;
  }
  cnt = 0;
  for (int i = n; i > 0; i--) {
    if (cnt == 0 || a[i] != s[cnt]) {
      if (cnt && a[i] > s[cnt]) {
        cout << "NO" << endl;
        return 0;
      }
      s[++cnt] = a[i];
    } else
      cnt--;
  }
  if (cnt > 1)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
  return 0;
}
