#include <bits/stdc++.h>
using namespace std;
long long tar[500005];
long long arr[500005];
int main() {
  long long n;
  cin >> n;
  long long idx = 0;
  map<long long, long long> mp;
  arr[0] = 0;
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      pq;
  long long mini;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    pq.push({arr[i], i});
  }
  while (!pq.empty()) {
    mini = pq.top().first;
    idx = pq.top().second;
    pq.pop();
    long long cnt = 0;
    int i = idx, j = idx - 1;
    if (mp[idx] > 0) continue;
    for (i = idx; i <= n; i++) {
      if (tar[i] != 0) {
        cnt += tar[i];
        i += (tar[i] - 1);
        continue;
      }
      if (arr[i] != mini) break;
      mp[i]++;
      cnt++;
    }
    for (j = idx - 1; j >= 1; j--) {
      if (tar[j] != 0) {
        cnt += tar[j];
        j -= tar[j];
        j++;
        continue;
      }
      if (arr[j] != mini) break;
      mp[j]++;
      cnt++;
    }
    if (cnt % 2 && cnt != n) {
      cout << "NO";
      return 0;
    }
    if (cnt % 2) break;
    if (i - 1 >= 0) tar[i - 1] = cnt;
    tar[j + 1] = cnt;
  }
  cout << "YES";
  return 0;
}
