#include <bits/stdc++.h>
using namespace std;
void File() {
  freopen("output.txt", "w", stdout);
  freopen("input.txt", "r", stdin);
}
void fast() {
  ios::sync_with_stdio(NULL);
  cout.tie(NULL);
  cin.tie(NULL);
}
const double pi = 2 * acos(0.0);
const int nn = 2e5 + 5;
const int oo = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int main() {
  fast();
  int n;
  cin >> n;
  int mx = -1;
  stack<int> ans;
  while (n--) {
    int x;
    cin >> x;
    mx = max(mx, x);
    if (ans.empty())
      ans.push(x);
    else if (ans.top() == x)
      ans.pop();
    else if (x > ans.top())
      return cout << "NO\n", 0;
    else
      ans.push(x);
  }
  if (((int)((ans).size())) > 1)
    cout << "NO\n";
  else if (ans.empty())
    cout << "YES\n";
  else if (ans.top() == mx)
    cout << "YES\n";
  else
    cout << "NO\n";
  return 0;
}
