#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16000000")
#pragma GCC optimize("O4")
using namespace std;
double eps = 1e-10;
int n;
int arr[200005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin.sync_with_stdio(0);
  cout.sync_with_stdio(0);
  cin >> n;
  int hmax = -1;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    hmax = max(hmax, arr[i]);
  }
  arr[0] = arr[n + 1] = hmax;
  vector<pair<int, int>> s;
  for (int i = 0; i <= n + 1; i++) {
    if (arr[i] == hmax && s.size() && s.back().first != hmax) {
      cout << "NO";
      return 0;
    }
    if (s.empty() || s.back().first > arr[i]) {
      s.push_back({arr[i], i});
    } else {
      if ((i - s.back().second + (arr[i] == s.back().first)) & 1) {
        cout << "NO";
        return 0;
      } else {
        s.pop_back();
      }
    }
  }
  cout << "YES";
  return 0;
}
