1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <iostream>
#include <vector>
#include <climits>
#include <set>
#include <map>
namespace alg {
using namespace std;
namespace num {
template < typename T >
bool SIGN() {
  return !(T(-1) > 0);
}
template < typename T >
T MAX() {
  return SIGN< T >() ? static_cast< T >(~static_cast< T >(T(1) << (sizeof(T) * CHAR_BIT - 1))) : T(-1);
}
}
namespace btr {
template < typename T >
T Height(T leaf_count) {
  T result = !!--leaf_count;
  T shift = static_cast< T >(sizeof(T) * CHAR_BIT);
  while (shift)
    if (leaf_count >= (T(1) << (shift >>= 1))) {
      result = T(result + shift);
      leaf_count = T(leaf_count >> shift);
    }
  return result;
}
template < typename T >
T LeafCount(T height) {
  return T(1) << height;
}
template < typename T >
T NodeCount(T height) {
  return LeafCount(++height);
}
template < typename T >
T Parent(T node) {
  return node >> 1;
}
enum {
  LEFT,
  RIGHT
};
template < typename T >
T Child(T node, int direction) {
  return T((node << 1) + (direction != 0));
}
template < typename T >
int Side(T node) {
  return static_cast< int >(node & T(1));
}
template < typename T >
T Toggle(T node) {
  return node ^ T(1);
}
template < typename T >
struct Walk {
  enum {
    COVER,
    ENTER,
    LEAVE
  };
  T node, level, left, right, count;
  struct
  {
    T left, right, count;
  } area;
  int mode;
  Walk(T height, T start, T count_ = 1)
      : node(0), level(0),
        left(start), right(left + count_),
        count(count_),
        mode(LEAVE) {
    area.left = area.right = T(0);
    area.left -= (area.count = LeafCount(height));
    --area.right;
    --right;
  }
  T step() {
    while (1) {
      switch (mode) {
      case ENTER:
        node = Child(node, LEFT);
        area.count >>= 1;
        area.right -= area.count;
        ++level;
      /* fall through */
      case LEAVE + 1:
        if (area.left <= right && left <= area.right) {
          mode = area.left < left || right < area.right;
          break;
        }
      /* fall through */
      default:
        if (Side(node) == LEFT) {
          area.left += area.count;
          area.right += area.count;
          node = Toggle(node);
          mode = LEAVE + 1;
          continue;
        }
        node = Parent(node);
        area.left -= area.count;
        area.count = T(area.count << 1);
        --level;
        mode = LEAVE;
      }
      break;
    }
    return node;
  }
};
}
template < typename V >
V &scaler(V const &v, V &s, V &m, size_t *diffCountPtr = 0) {
  size_t index = 0;
  typedef std::map< typename V::value_type, size_t > map_t;
  map_t z;
  for (typename V::const_iterator it = v.begin(); it != v.end(); z[*it++] = 0) {
  }
  if (diffCountPtr)
    *diffCountPtr = z.size();
  m.resize(z.size());
  for (typename map_t::iterator it = z.begin(); it != z.end(); ++it)
    m[it->second = index++] = it->first;
  s.resize(index = v.size());
  while (index--)
    s[index] = static_cast< typename V::value_type >(z[v[index]]);
  return s;
}
template < typename V >
V &scaler(V const &v, V &s, size_t *diffCountPtr = 0) {
  V m;
  return scaler(v, s, m, diffCountPtr);
}
template < typename V >
V scaler(V const &v, size_t *diffCountPtr = 0) {
  V s;
  return scaler(v, s, diffCountPtr);
}
}
using namespace alg;
struct Tree {
  size_t leaf_count, height;
  vector< std::set< size_t > > vs;
  Tree(size_t leaf_count_)
      : leaf_count(leaf_count_),
        height(btr::Height(leaf_count)),
        vs(btr::NodeCount(height)) {
  }
  void set(size_t id, size_t value) {
    btr::Walk< size_t > walk(height, value);
    while (walk.step())
      if (walk.mode != btr::Walk< size_t >::ENTER)
        vs[walk.node].insert(id);
  }
  size_t get(size_t id, size_t value, bool before) const {
    size_t result = num::MAX< size_t >();
    size_t count = leaf_count - ++value;
    if (count) {
      btr::Walk< size_t > walk(height, value, count);
      while (walk.step())
        if (walk.mode == btr::Walk< size_t >::COVER) {
          std::set< size_t > const &s = vs[walk.node];
          std::set< size_t >::iterator it =
              before ? s.begin() : s.upper_bound(id);
          if (it != s.end())
            result = min(result, *it);
        }
    }
    return result;
  }
  size_t get(size_t id, size_t value) const {
    size_t result = get(id, value, false);
    return (result == num::MAX< size_t >()) ? get(id, value, true) : result;
  }
};
struct G {
  vector< vector< size_t > > v;
  G(size_t n) : v(n) {
  }
  void connect(size_t a, size_t b) {
    v[a].push_back(b);
  }
  vector< size_t > toposort() const {
    vector< size_t > result, in_degree(v.size()), q;
    for (size_t i = 0; i < v.size(); ++i)
      for (size_t j = 0; j < v[i].size(); ++j)
        in_degree[v[i][j]]++;
    for (size_t i = 0; i < v.size(); ++i)
      if (!in_degree[i])
        q.push_back(i);
    while (q.size()) {
      size_t i = q.back();
      q.pop_back();
      result.push_back(i);
      for (size_t j = 0; j < v[i].size(); ++j)
        if (!--in_degree[v[i][j]])
          q.push_back(v[i][j]);
    }
    return result;
  }
  size_t lcs() const {
    size_t result = 0;
    vector< size_t > ts = toposort(), dist(v.size());
    for (size_t i = 0; i < v.size(); ++i) {
      size_t a = ts[i];
      for (size_t j = 0; j < v[a].size(); ++j) {
        size_t b = v[a][j];
        dist[b] = max(dist[b], dist[a] + 1);
      }
      result = max(result, dist[a]);
    }
    return result;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  size_t n, i = num::MAX< size_t >(), a;
  cin >> n;
  vector< size_t > v;
  while (n--) {
    cin >> a;
    if (a != i)
      v.push_back(i = a);
  }
  while (v.size() > 1 && v.front() == v.back())
    v.pop_back();
  v = scaler(v, &i);
  Tree t(i);
  for (i = 0; i < v.size(); ++i)
    t.set(i, v[i]);
  G g(v.size());
  for (i = 0; i < v.size(); ++i)
    if ((n = t.get(i, v[i])) != num::MAX< size_t >())
      g.connect(i, n);
  cout << (g.lcs() + 1) << '\n';
}