#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   int n, a[N], b[N];   vector<int> vec[N];   set<pair<int, int> > S;   struct node {     int cl, cr, mx, vl, vr, v;     node() {}     node(int c) {       cl = cr = c;       mx = vl = vr = v = 1;     }   } t[N * 4];   node operator+(node a, node b) {     node ans;     ans.cl = a.cl;     ans.cr = b.cr;     ans.mx = max(a.mx, b.mx);     ans.vl = a.vl;     ans.vr = b.vr;     ans.v = a.v + b.v;     if (a.cr ^ b.cl) {       ans.mx = max(ans.mx, a.vr + b.vl);       if (a.v == a.vl) ans.vl += b.vl;       if (b.v == b.vr) ans.vr += a.vr;     }     return ans;   }   void build(int k, int l, int r) {     if (l == r) {       t[k] = node(0);       return;     }     int mid = (l + r) / 2;     build(k * 2, l, mid);     build(k * 2 + 1, mid + 1, r);     t[k] = t[k * 2] + t[k * 2 + 1];   }   void change(int k, int l, int r, int p) {     if (l == r) {       t[k] = node(1);       return;     }     int mid = (l + r) / 2;     if (p <= mid)       change(k * 2, l, mid, p);     else       change(k * 2 + 1, mid + 1, r, p);     t[k] = t[k * 2] + t[k * 2 + 1];   }   struct YYN {     int t[N * 4], nn;     void build(int n) {       for (nn = 1; nn <= n; nn <<= 1)         ;       for (int i = (int)(0); i <= (int)(2 * nn - 1); i++) t[i] = 0;     }     void change(int x, int y, int v) {       x += nn - 1;       y += nn + 1;       for (; x ^ y ^ 1; x >>= 1, y >>= 1) {         if (!(x & 1)) t[x ^ 1] = max(t[x ^ 1], v);         if (y & 1) t[y ^ 1] = max(t[y ^ 1], v);       }     }     void output() {       for (int i = (int)(1); i <= (int)(nn - 1); i++) {         t[i * 2] = max(t[i * 2], t[i]);         t[i * 2 + 1] = max(t[i * 2 + 1], t[i]);       }       for (int i = (int)(1); i <= (int)(n); i++) printf( %d  , t[i + nn]);       puts(  );     }   } RE;   void flip(int x) {     assert(a[x] == 0);     if (x == 1 || a[x] == a[x - 1]) S.erase(pair<int, int>(x, a[x]));     if (x == n || a[x] == a[x + 1]) S.erase(pair<int, int>(x + 1, a[x]));     a[x] ^= 1;     change(1, 1, n, x);     if (x == 1 || a[x] == a[x - 1]) S.insert(pair<int, int>(x, a[x]));     if (x == n || a[x] == a[x + 1]) S.insert(pair<int, int>(x + 1, a[x]));   }   void updateee(int x, int v) {     auto it = S.lower_bound(pair<int, int>(x, -1));     auto pre = it, suf = it;     pre--;     if (pre->second == 1 && suf->second == 1)       RE.change(pre->first, suf->first - 1, v);     else if (pre->second == 1 && suf->second == 0)       RE.change(pre->first, (pre->first + suf->first) / 2 - 1, v);     else if (pre->second == 0 && suf->second == 1)       RE.change((pre->first + suf->first) / 2, suf->first - 1, v);   }   void updatee(int x, int v) {     auto it = S.lower_bound(pair<int, int>(x, -1));     auto pre = it, suf = it;     if (x != 1) {       --pre;       if (pre->second == 1)         RE.change(pre->first, it->first - 1, v);       else         RE.change((pre->first + it->first) / 2, it->first - 1, v);     }     if (x != n + 1) {       ++suf;       if (suf->second == 1)         RE.change(it->first, suf->first - 1, v);       else         RE.change(it->first, (it->first + suf->first) / 2 - 1, v);     }   }   void update(int x, int v) {     if (x == 1 || a[x] == a[x - 1])       updatee(x, v);     else       updateee(x, v);     if (x == n || a[x] == a[x + 1])       updatee(x + 1, v);     else       updateee(x + 1, v);   }   int main() {     scanf( %d , &n);     for (int i = (int)(1); i <= (int)(n); i++) scanf( %d , &a[i]);     for (int i = (int)(1); i <= (int)(n); i++) b[i] = a[i];     sort(b + 1, b + n + 1);     *b = unique(b + 1, b + n + 1) - b - 1;     for (int i = (int)(1); i <= (int)(n); i++) {       a[i] = lower_bound(b + 1, b + *b + 1, a[i]) - b;       vec[a[i]].push_back(i);     }     memset(a, 0, sizeof(a));     build(1, 1, n);     RE.build(n);     for (int i = (int)(1); i <= (int)(n + 1); i++) S.insert(pair<int, int>(i, 0));     int ansT = 0;     for (int i = (int)(*b); i >= (int)(1); i--) {       for (auto j : vec[i]) flip(j);       for (auto j : vec[i]) update(j, b[i]);       ansT = max(ansT, t[1].mx);     }     printf( %d n , (ansT - 1) / 2);     RE.output();   }