Vasya has an array of integers of length n.

Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13, 13, 7, 7, 7, 2, 2, 2], then after one operation it becomes [13, 13, 2, 2, 2].

Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.

The second line contains a sequence a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — Vasya's array.


-----Output-----

Print the number of operations Vasya should make to remove all elements from the array.


-----Examples-----
Input
4
2 5 5 2

Output
2

Input
5
6 3 4 1 5

Output
5

Input
8
4 4 4 2 2 100 100 100

Output
3

Input
6
10 10 50 10 50 50

Output
4



-----Note-----

In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty.

In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array.

In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second — all integers 100, in the third — all integers 2.

In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50, 10, 50, 50]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50, 10]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that.

Source: codeforces | Difficulty: HARD
Tags: Amortized analysis

Implementation

Flows and cuts

Spanning trees

Data structures


Solutions:
from sys import stdin
from heapq import heappop, heappush, heapify

def main():
	n = int(stdin.readline())
	a = stdin.readline().split()
	q = []
	p = 0
	c = 0
	l = [0] * (n + 1)
	r = [0] * (n + 1)
	k = [0] * (n + 1)
	pa = [0] * (n + 1)
	for (i, x) in enumerate(a):
		if x == a[p]:
			c += 1
		else:
			l[p] = p - 1
			k[p] = k[i - 1] = c
			pa[p] = i - 1
			pa[i - 1] = p
			r[i - 1] = i
			q.append((-c, p))
			p = i
			c = 1
	q.append((-c, p))
	l[p] = p - 1
	k[p] = k[n - 1] = c
	pa[p] = n - 1
	pa[n - 1] = p
	r[n - 1] = n
	heapify(q)
	ans = 0
	while len(q):
		(c, p) = heappop(q)
		c = -c
		if k[p] > c:
			continue
		ans += 1
		ls = l[p]
		rs = r[pa[p]]
		if ls >= 0 and rs < n and (a[ls] == a[rs]):
			nc = k[ls] + k[rs]
			(nl, nr) = (pa[ls], pa[rs])
			k[nl] = k[nr] = k[ls] = k[rs] = nc
			pa[nr] = nl
			pa[nl] = nr
			heappush(q, (-nc, nl))
		else:
			if ls >= 0:
				r[ls] = rs
			if rs < n:
				l[rs] = ls
	print(ans)
main()


from sys import stdin
import collections

def doc(s):
	if hasattr(s, '__call__'):
		s = s.__doc__

	def f(g):
		g.__doc__ = s
		return g
	return f

class heapdict(collections.MutableMapping):
	__marker = object()

	@staticmethod
	def _parent(i):
		return i - 1 >> 1

	@staticmethod
	def _left(i):
		return (i << 1) + 1

	@staticmethod
	def _right(i):
		return i + 1 << 1

	def __init__(self, *args, **kw):
		self.heap = []
		self.d = {}
		self.update(*args, **kw)

	@doc(dict.clear)
	def clear(self):
		self.heap.clear()
		self.d.clear()

	@doc(dict.__setitem__)
	def __setitem__(self, key, value):
		if key in self.d:
			self.pop(key)
		wrapper = [value, key, len(self)]
		self.d[key] = wrapper
		self.heap.append(wrapper)
		self._decrease_key(len(self.heap) - 1)

	def _min_heapify(self, i):
		l = self._left(i)
		r = self._right(i)
		n = len(self.heap)
		if l < n and self.heap[l][0] < self.heap[i][0]:
			low = l
		else:
			low = i
		if r < n and self.heap[r][0] < self.heap[low][0]:
			low = r
		if low != i:
			self._swap(i, low)
			self._min_heapify(low)

	def _decrease_key(self, i):
		while i:
			parent = self._parent(i)
			if self.heap[parent][0] < self.heap[i][0]:
				break
			self._swap(i, parent)
			i = parent

	def _swap(self, i, j):
		(self.heap[i], self.heap[j]) = (self.heap[j], self.heap[i])
		self.heap[i][2] = i
		self.heap[j][2] = j

	@doc(dict.__delitem__)
	def __delitem__(self, key):
		wrapper = self.d[key]
		while wrapper[2]:
			parentpos = self._parent(wrapper[2])
			parent = self.heap[parentpos]
			self._swap(wrapper[2], parent[2])
		self.popitem()

	@doc(dict.__getitem__)
	def __getitem__(self, key):
		return self.d[key][0]

	@doc(dict.__iter__)
	def __iter__(self):
		return iter(self.d)

	def popitem(self):
		wrapper = self.heap[0]
		if len(self.heap) == 1:
			self.heap.pop()
		else:
			self.heap[0] = self.heap.pop(-1)
			self.heap[0][2] = 0
			self._min_heapify(0)
		del self.d[wrapper[1]]
		return (wrapper[1], wrapper[0])

	@doc(dict.__len__)
	def __len__(self):
		return len(self.d)

	def peekitem(self):
		return (self.heap[0][1], self.heap[0][0])
del doc
__all__ = ['heapdict']

def create_necessities(arr, n):
	size_heap = heapdict()
	link = dict()
	arr.append(-1)
	count = 1
	for i in range(n + 1):
		if i == 0:
			leader = arr[i]
			size = 1
		elif arr[i] == arr[i - 1]:
			size += 1
		else:
			size_heap[count] = -(size * 10 ** 14) - (n - i)
			link[count] = leader
			count += 1
			leader = arr[i]
			size = 1
	next_node = dict()
	prev_node = dict()
	for i in link:
		if i == 1:
			prev_node[i] = None
			if i + 1 < count:
				next_node[i] = i + 1
			else:
				next_node[i] = None
		elif i == count:
			break
		else:
			prev_node[i] = i - 1
			if i + 1 < count:
				next_node[i] = i + 1
			else:
				next_node[i] = None
	return (link, size_heap, prev_node, next_node)
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
(link, size_heap, prev_node, next_node) = create_necessities(arr, len(arr))
op = 0
while len(size_heap) > 0:
	(node, size) = size_heap.popitem()
	if prev_node[node] != None and next_node[node] != None and (link[prev_node[node]] == link[next_node[node]]):
		if prev_node[node] in size_heap and next_node[node] in size_heap:
			temp1 = size_heap[prev_node[node]]
			t1 = -temp1 % 10 ** 14
			temp2 = size_heap[next_node[node]]
			t2 = -temp2 % 10 ** 14
			size_heap[prev_node[node]] = -float('inf')
			size_heap.popitem()
			size_heap[next_node[node]] = -float('inf')
			size_heap.popitem()
			size_heap[prev_node[node]] = temp1 + temp2 + t2
			next_node[prev_node[node]] = next_node[next_node[node]]
			if next_node[next_node[node]] != None:
				prev_node[next_node[next_node[node]]] = prev_node[node]
		else:
			prev_node[next_node[node]] = prev_node[node]
			next_node[prev_node[node]] = next_node[node]
	else:
		prev_node[next_node[node]] = prev_node[node]
		next_node[prev_node[node]] = next_node[node]
	op += 1
print(op)


import heapq
n = int(input())
a = list(map(int, input().split()))
d = {}
(pre, l) = (None, 0)
seg = []
for (i, x) in enumerate(a):
	if pre is None:
		pre = x
		l = 1
	elif x == pre:
		l += 1
	else:
		seg.append([l, pre])
		pre = x
		l = 1
	if i == len(a) - 1:
		seg.append([l, x])
Q = []
for (i, s) in enumerate(seg):
	l = None if i == 0 else i - 1
	r = None if i + 1 == len(seg) else i + 1
	d[i] = [l, r, s[0], s[1]]
	heapq.heappush(Q, (-s[0], i))
cnt = 0
while len(Q) > 0:
	(length, idx) = heapq.heappop(Q)
	length = -length
	if d[idx][2] != length:
		continue
	(l, r, length, val) = d[idx]
	d[idx][0] = 0
	cnt += 1
	if l is None and r is None:
		break
	elif l is None:
		d[r][0] = None
	elif r is None:
		d[l][1] = None
	elif d[l][3] == d[r][3]:
		d[l][1] = d[r][1]
		d[l][2] += d[r][2]
		d[r][2] = 0
		if d[r][1] is not None:
			nnr = d[r][1]
			d[nnr][0] = l
		heapq.heappush(Q, (-d[l][2], l))
	else:
		d[l][1] = r
		d[r][0] = l
print(cnt)


from heapq import *
from random import randint
import sys
n = int(input())
a = list(map(int, input().split()))
q = []
p = 0
c = 0
l = [0] * (n + 1)
r = [0] * (n + 1)
k = [0] * (n + 1)
pa = [0] * (n + 1)
for (i, x) in enumerate(a):
	if x == a[p]:
		c += 1
	else:
		l[p] = p - 1
		k[p] = k[i - 1] = c
		pa[p] = i - 1
		pa[i - 1] = p
		r[i - 1] = i
		q.append((-c, p))
		p = i
		c = 1
q.append((-c, p))
l[p] = p - 1
k[p] = k[n - 1] = c
pa[p] = n - 1
pa[n - 1] = p
r[n - 1] = n
heapify(q)
ans = 0
while len(q):
	(c, p) = heappop(q)
	c = -c
	if k[p] > c:
		continue
	ans += 1
	ls = l[p]
	rs = r[pa[p]]
	if ls >= 0 and rs < n and (a[ls] == a[rs]):
		nc = k[ls] + k[rs]
		(nl, nr) = (pa[ls], pa[rs])
		k[nl] = k[nr] = k[ls] = k[rs] = nc
		pa[nr] = nl
		pa[nl] = nr
		heappush(q, (-nc, nl))
	else:
		if ls >= 0:
			r[ls] = rs
		if rs < n:
			l[rs] = ls
print(ans)