#include <bits/stdc++.h>
using namespace std;
const long double max2 = 1e9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cerr.tie(NULL);
  int tab;
  auto geti = [&]() {
    cin >> tab;
    return tab;
  };
  ;
  long long int n;
  cin >> n;
  if (n == 1) {
    cout << "YES\n";
    return 0;
  }
  long long int arr[n];
  long long int maha_max = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] >= maha_max) {
      maha_max = arr[i];
    }
  }
  stack<long long int> que;
  long long int i, d;
  long long int maxy = 0;
  for (i = 0; i + 1 < n; i++) {
    d = abs(arr[i + 1] - arr[i]);
    if (d == 0) {
      if (arr[i] >= maxy) {
        maxy = arr[i];
      }
      i++;
    } else {
      if (que.size() != 0) {
        long long int h = que.top();
        d = abs(arr[i] - arr[h]);
        if (d == 0 && arr[i] >= maxy) {
          que.pop();
          maxy = 0;
          continue;
        } else {
          maxy = 0;
          que.push(i);
        }
      } else {
        que.push(i);
        maxy = 0;
      }
    }
  }
  if (i + 1 == n) {
    if (que.size() != 0) {
      long long int h = que.top();
      d = abs(arr[n - 1] - arr[h]);
      if (d == 0 && arr[i] >= maxy) {
        que.pop();
      } else {
        que.push(i);
      }
    } else {
      que.push(n - 1);
    }
  }
  if (que.size() == 0) {
    cout << "YES\n";
  } else {
    long long int check = 0;
    while (!que.empty()) {
      long long int h = que.top();
      que.pop();
      if (arr[h] != maha_max) {
        cout << "NO" << endl;
        return 0;
      }
    }
    cout << "YES\n";
  }
  return 0;
}
