QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#439103#4954. Eliminating BallonssergiolozavTL 839ms88132kbPython348.5kb2024-06-11 16:11:162024-06-11 16:11:17

Judging History

你现在查看的是最新测评结果

  • [2024-06-11 16:11:17]
  • 评测
  • 测评结果:TL
  • 用时:839ms
  • 内存:88132kb
  • [2024-06-11 16:11:16]
  • 提交

answer

import timeit

class Stopwatch:
    hide = False

    def __init__(self, name):
        self.time = 0
        self.calls = 0
        self.name = name
        self.current_execution = 0
    
    def start_cumulative(self):
        if Stopwatch.hide:
            return

        self.current_execution = timeit.default_timer()
        self.calls += 1
    
    def stop_cumulative(self):
        if Stopwatch.hide:
            return

        assert self.current_execution != 0, "'start_cumulative' must be called before 'stop_cumulative'"
        elapsed = timeit.default_timer() - self.current_execution
        self.current_execution = 0
        self.time += elapsed
    
    def print(self):
        if not Stopwatch.hide:
            print(self)

    def __repr__(self) -> str:
        return f"{self.name} - {self.time}s - {self.calls}"
    


############### TEMPLATE FOR SORTED LIST IN PYTHON #######################
 
import sys
import traceback
from bisect import bisect_left, bisect_right, insort
from itertools import chain, repeat, starmap
from math import log
from operator import add, eq, ne, gt, ge, lt, le, iadd
from textwrap import dedent
from collections.abc import Sequence, MutableSequence
from functools import reduce
 
class SortedList(MutableSequence):
    """Sorted list is a sorted mutabƒedpandle sequence.
 
    Sorted list values are maintained in sorted order.
 
    Sorted list values must be comparable. The total ordering of values must
    not change while they are stored in the sorted list.
 
    Methods for adding values:
 
    * :func:`SortedList.add`
    * :func:`SortedList.update`
    * :func:`SortedList.__add__`
    * :func:`SortedList.__iadd__`
    * :func:`SortedList.__mul__`
    * :func:`SortedList.__imul__`
 
    Methods for removing values:
 
    * :func:`SortedList.clear`
    * :func:`SortedList.discard`
    * :func:`SortedList.remove`
    * :func:`SortedList.pop`
    * :func:`SortedList.__delitem__`
 
    Methods for looking up values:
 
    * :func:`SortedList.bisect_left`
    * :func:`SortedList.bisect_right`
    * :func:`SortedList.count`
    * :func:`SortedList.index`
    * :func:`SortedList.__contains__`
    * :func:`SortedList.__getitem__`
 
    Methods for iterating values:
 
    * :func:`SortedList.irange`
    * :func:`SortedList.islice`
    * :func:`SortedList.__iter__`
    * :func:`SortedList.__reversed__`
 
    Methods for miscellany:
 
    * :func:`SortedList.copy`
    * :func:`SortedList.__len__`
    * :func:`SortedList.__repr__`
    * :func:`SortedList._check`
    * :func:`SortedList._reset`
 
    Sorted lists use lexicographical ordering semantics when compared to other
    sequences.
 
    Some methods of mutable sequences are not supported and will raise
    not-implemented error.
 
    """
 
    DEFAULT_LOAD_FACTOR = 1000
 
    def __init__(self, iterable=None, key=None):
        """Initialize sorted list instance.
 
        Optional `iterable` argument provides an initial iterable of values to
        initialize the sorted list.
 
        Runtime complexity: `O(n*log(n))`
 
        >>> sl = SortedList()
        >>> sl
        SortedList([])
        >>> sl = SortedList([3, 1, 2, 5, 4])
        >>> sl
        SortedList([1, 2, 3, 4, 5])
 
        :param iterable: initial values (optional)
 
        """
        assert key is None
        self._len = 0
        self._load = self.DEFAULT_LOAD_FACTOR
        self._lists = []
        self._maxes = []
        self._index = []
        self._offset = 0
 
        if iterable is not None:
            self._update(iterable)
 
    @property
    def key(self):  # pylint: disable=useless-return
        """Function used to extract comparison key from values.
 
        Sorted list compares values directly so the key function is none.
 
        """
        return None
 
    def _reset(self, load):
        """Reset sorted list load factor.
 
        The `load` specifies the load-factor of the list. The default load
        factor of 1000 works well for lists from tens to tens-of-millions of
        values. Good practice is to use a value that is the cube root of the
        list size. With billions of elements, the best load factor depends on
        your usage. It's best to leave the load factor at the default until you
        start benchmarking.
 
        See :doc:`implementation` and :doc:`performance-scale` for more
        information.
 
        Runtime complexity: `O(n)`
 
        :param int load: load-factor for sorted list sublists
 
        """
        values = reduce(iadd, self._lists, [])
        self._clear()
        self._load = load
        self._update(values)
 
    def clear(self):
        """Remove all values from sorted list.
 
        Runtime complexity: `O(n)`
 
        """
        self._len = 0
        del self._lists[:]
        del self._maxes[:]
        del self._index[:]
        self._offset = 0
 
    _clear = clear
 
    def add(self, value):
        """Add `value` to sorted list.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList()
        >>> sl.add(3)
        >>> sl.add(1)
        >>> sl.add(2)
        >>> sl
        SortedList([1, 2, 3])
 
        :param value: value to add to sorted list
 
        """
        _lists = self._lists
        _maxes = self._maxes
 
        if _maxes:
            pos = bisect_right(_maxes, value)
 
            if pos == len(_maxes):
                pos -= 1
                _lists[pos].append(value)
                _maxes[pos] = value
            else:
                insort(_lists[pos], value)
 
            self._expand(pos)
        else:
            _lists.append([value])
            _maxes.append(value)
 
        self._len += 1
 
    def _expand(self, pos):
        """Split sublists with length greater than double the load-factor.
 
        Updates the index when the sublist length is less than double the load
        level. This requires incrementing the nodes in a traversal from the
        leaf node to the root. For an example traversal see
        ``SortedList._loc``.
 
        """
        _load = self._load
        _lists = self._lists
        _index = self._index
 
        if len(_lists[pos]) > (_load << 1):
            _maxes = self._maxes
 
            _lists_pos = _lists[pos]
            half = _lists_pos[_load:]
            del _lists_pos[_load:]
            _maxes[pos] = _lists_pos[-1]
 
            _lists.insert(pos + 1, half)
            _maxes.insert(pos + 1, half[-1])
 
            del _index[:]
        else:
            if _index:
                child = self._offset + pos
                while child:
                    _index[child] += 1
                    child = (child - 1) >> 1
                _index[0] += 1
 
    def update(self, iterable):
        """Update sorted list by adding all values from `iterable`.
 
        Runtime complexity: `O(k*log(n))` -- approximate.
 
        >>> sl = SortedList()
        >>> sl.update([3, 1, 2])
        >>> sl
        SortedList([1, 2, 3])
 
        :param iterable: iterable of values to add
 
        """
        _lists = self._lists
        _maxes = self._maxes
        values = sorted(iterable)
 
        if _maxes:
            if len(values) * 4 >= self._len:
                _lists.append(values)
                values = reduce(iadd, _lists, [])
                values.sort()
                self._clear()
            else:
                _add = self.add
                for val in values:
                    _add(val)
                return
 
        _load = self._load
        _lists.extend(
            values[pos : (pos + _load)] for pos in range(0, len(values), _load)
        )
        _maxes.extend(sublist[-1] for sublist in _lists)
        self._len = len(values)
        del self._index[:]
 
    _update = update
 
    def __contains__(self, value):
        """Return true if `value` is an element of the sorted list.
 
        ``sl.__contains__(value)`` <==> ``value in sl``
 
        Runtime complexity: `O(log(n))`
 
        >>> sl = SortedList([1, 2, 3, 4, 5])
        >>> 3 in sl
        True
 
        :param value: search for value in sorted list
        :return: true if `value` in sorted list
 
        """
        _maxes = self._maxes
 
        if not _maxes:
            return False
 
        pos = bisect_left(_maxes, value)
 
        if pos == len(_maxes):
            return False
 
        _lists = self._lists
        idx = bisect_left(_lists[pos], value)
 
        return _lists[pos][idx] == value
 
    def discard(self, value):
        """Remove `value` from sorted list if it is a member.
 
        If `value` is not a member, do nothing.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([1, 2, 3, 4, 5])
        >>> sl.discard(5)
        >>> sl.discard(0)
        >>> sl == [1, 2, 3, 4]
        True
 
        :param value: `value` to discard from sorted list
 
        """
        _maxes = self._maxes
 
        if not _maxes:
            return
 
        pos = bisect_left(_maxes, value)
 
        if pos == len(_maxes):
            return
 
        _lists = self._lists
        idx = bisect_left(_lists[pos], value)
 
        if _lists[pos][idx] == value:
            self._delete(pos, idx)
 
    def remove(self, value):
        """Remove `value` from sorted list; `value` must be a member.
 
        If `value` is not a member, raise ValueError.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([1, 2, 3, 4, 5])
        >>> sl.remove(5)
        >>> sl == [1, 2, 3, 4]
        True
        >>> sl.remove(0)
        Traceback (most recent call last):
          ...
        ValueError: 0 not in list
 
        :param value: `value` to remove from sorted list
        :raises ValueError: if `value` is not in sorted list
 
        """
        _maxes = self._maxes
 
        if not _maxes:
            raise ValueError("{0!r} not in list".format(value))
 
        pos = bisect_left(_maxes, value)
 
        if pos == len(_maxes):
            raise ValueError("{0!r} not in list".format(value))
 
        _lists = self._lists
        idx = bisect_left(_lists[pos], value)
 
        if _lists[pos][idx] == value:
            self._delete(pos, idx)
        else:
            raise ValueError("{0!r} not in list".format(value))
 
    def _delete(self, pos, idx):
        """Delete value at the given `(pos, idx)`.
 
        Combines lists that are less than half the load level.
 
        Updates the index when the sublist length is more than half the load
        level. This requires decrementing the nodes in a traversal from the
        leaf node to the root. For an example traversal see
        ``SortedList._loc``.
 
        :param int pos: lists index
        :param int idx: sublist index
 
        """
        _lists = self._lists
        _maxes = self._maxes
        _index = self._index
 
        _lists_pos = _lists[pos]
 
        del _lists_pos[idx]
        self._len -= 1
 
        len_lists_pos = len(_lists_pos)
 
        if len_lists_pos > (self._load >> 1):
            _maxes[pos] = _lists_pos[-1]
 
            if _index:
                child = self._offset + pos
                while child > 0:
                    _index[child] -= 1
                    child = (child - 1) >> 1
                _index[0] -= 1
        elif len(_lists) > 1:
            if not pos:
                pos += 1
 
            prev = pos - 1
            _lists[prev].extend(_lists[pos])
            _maxes[prev] = _lists[prev][-1]
 
            del _lists[pos]
            del _maxes[pos]
            del _index[:]
 
            self._expand(prev)
        elif len_lists_pos:
            _maxes[pos] = _lists_pos[-1]
        else:
            del _lists[pos]
            del _maxes[pos]
            del _index[:]
 
    def _loc(self, pos, idx):
        """Convert an index pair (lists index, sublist index) into a single
        index number that corresponds to the position of the value in the
        sorted list.
 
        Many queries require the index be built. Details of the index are
        described in ``SortedList._build_index``.
 
        Indexing requires traversing the tree from a leaf node to the root. The
        parent of each node is easily computable at ``(pos - 1) // 2``.
 
        Left-child nodes are always at odd indices and right-child nodes are
        always at even indices.
 
        When traversing up from a right-child node, increment the total by the
        left-child node.
 
        The final index is the sum from traversal and the index in the sublist.
 
        For example, using the index from ``SortedList._build_index``::
 
            _index = 14 5 9 3 2 4 5
            _offset = 3
 
        Tree::
 
                 14
              5      9
            3   2  4   5
 
        Converting an index pair (2, 3) into a single index involves iterating
        like so:
 
        1. Starting at the leaf node: offset + alpha = 3 + 2 = 5. We identify
           the node as a left-child node. At such nodes, we simply traverse to
           the parent.
 
        2. At node 9, position 2, we recognize the node as a right-child node
           and accumulate the left-child in our total. Total is now 5 and we
           traverse to the parent at position 0.
 
        3. Iteration ends at the root.
 
        The index is then the sum of the total and sublist index: 5 + 3 = 8.
 
        :param int pos: lists index
        :param int idx: sublist index
        :return: index in sorted list
 
        """
        if not pos:
            return idx
 
        _index = self._index
 
        if not _index:
            self._build_index()
 
        total = 0
 
        # Increment pos to point in the index to len(self._lists[pos]).
 
        pos += self._offset
 
        # Iterate until reaching the root of the index tree at pos = 0.
 
        while pos:
 
            # Right-child nodes are at odd indices. At such indices
            # account the total below the left child node.
 
            if not pos & 1:
                total += _index[pos - 1]
 
            # Advance pos to the parent node.
 
            pos = (pos - 1) >> 1
 
        return total + idx
 
    def _pos(self, idx):
        """Convert an index into an index pair (lists index, sublist index)
        that can be used to access the corresponding lists position.
 
        Many queries require the index be built. Details of the index are
        described in ``SortedList._build_index``.
 
        Indexing requires traversing the tree to a leaf node. Each node has two
        children which are easily computable. Given an index, pos, the
        left-child is at ``pos * 2 + 1`` and the right-child is at ``pos * 2 +
        2``.
 
        When the index is less than the left-child, traversal moves to the
        left sub-tree. Otherwise, the index is decremented by the left-child
        and traversal moves to the right sub-tree.
 
        At a child node, the indexing pair is computed from the relative
        position of the child node as compared with the offset and the remaining
        index.
 
        For example, using the index from ``SortedList._build_index``::
 
            _index = 14 5 9 3 2 4 5
            _offset = 3
 
        Tree::
 
                 14
              5      9
            3   2  4   5
 
        Indexing position 8 involves iterating like so:
 
        1. Starting at the root, position 0, 8 is compared with the left-child
           node (5) which it is greater than. When greater the index is
           decremented and the position is updated to the right child node.
 
        2. At node 9 with index 3, we again compare the index to the left-child
           node with value 4. Because the index is the less than the left-child
           node, we simply traverse to the left.
 
        3. At node 4 with index 3, we recognize that we are at a leaf node and
           stop iterating.
 
        4. To compute the sublist index, we subtract the offset from the index
           of the leaf node: 5 - 3 = 2. To compute the index in the sublist, we
           simply use the index remaining from iteration. In this case, 3.
 
        The final index pair from our example is (2, 3) which corresponds to
        index 8 in the sorted list.
 
        :param int idx: index in sorted list
        :return: (lists index, sublist index) pair
 
        """
        if idx < 0:
            last_len = len(self._lists[-1])
 
            if (-idx) <= last_len:
                return len(self._lists) - 1, last_len + idx
 
            idx += self._len
 
            if idx < 0:
                raise IndexError("list index out of range")
        elif idx >= self._len:
            raise IndexError("list index out of range")
 
        if idx < len(self._lists[0]):
            return 0, idx
 
        _index = self._index
 
        if not _index:
            self._build_index()
 
        pos = 0
        child = 1
        len_index = len(_index)
 
        while child < len_index:
            index_child = _index[child]
 
            if idx < index_child:
                pos = child
            else:
                idx -= index_child
                pos = child + 1
 
            child = (pos << 1) + 1
 
        return (pos - self._offset, idx)
 
    def _build_index(self):
        """Build a positional index for indexing the sorted list.
 
        Indexes are represented as binary trees in a dense array notation
        similar to a binary heap.
 
        For example, given a lists representation storing integers::
 
            0: [1, 2, 3]
            1: [4, 5]
            2: [6, 7, 8, 9]
            3: [10, 11, 12, 13, 14]
 
        The first transformation maps the sub-lists by their length. The
        first row of the index is the length of the sub-lists::
 
            0: [3, 2, 4, 5]
 
        Each row after that is the sum of consecutive pairs of the previous
        row::
 
            1: [5, 9]
            2: [14]
 
        Finally, the index is built by concatenating these lists together::
 
            _index = [14, 5, 9, 3, 2, 4, 5]
 
        An offset storing the start of the first row is also stored::
 
            _offset = 3
 
        When built, the index can be used for efficient indexing into the list.
        See the comment and notes on ``SortedList._pos`` for details.
 
        """
        row0 = list(map(len, self._lists))
 
        if len(row0) == 1:
            self._index[:] = row0
            self._offset = 0
            return
 
        head = iter(row0)
        tail = iter(head)
        row1 = list(starmap(add, zip(head, tail)))
 
        if len(row0) & 1:
            row1.append(row0[-1])
 
        if len(row1) == 1:
            self._index[:] = row1 + row0
            self._offset = 1
            return
 
        size = 2 ** (int(log(len(row1) - 1, 2)) + 1)
        row1.extend(repeat(0, size - len(row1)))
        tree = [row0, row1]
 
        while len(tree[-1]) > 1:
            head = iter(tree[-1])
            tail = iter(head)
            row = list(starmap(add, zip(head, tail)))
            tree.append(row)
 
        reduce(iadd, reversed(tree), self._index)
        self._offset = size * 2 - 1
 
    def __delitem__(self, index):
        """Remove value at `index` from sorted list.
 
        ``sl.__delitem__(index)`` <==> ``del sl[index]``
 
        Supports slicing.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList('abcde')
        >>> del sl[2]
        >>> sl
        SortedList(['a', 'b', 'd', 'e'])
        >>> del sl[:2]
        >>> sl
        SortedList(['d', 'e'])
 
        :param index: integer or slice for indexing
        :raises IndexError: if index out of range
 
        """
        if isinstance(index, slice):
            start, stop, step = index.indices(self._len)
 
            if step == 1 and start < stop:
                if start == 0 and stop == self._len:
                    return self._clear()
                elif self._len <= 8 * (stop - start):
                    values = self._getitem(slice(None, start))
                    if stop < self._len:
                        values += self._getitem(slice(stop, None))
                    self._clear()
                    return self._update(values)
 
            indices = range(start, stop, step)
 
            # Delete items from greatest index to least so
            # that the indices remain valid throughout iteration.
 
            if step > 0:
                indices = reversed(indices)
 
            _pos, _delete = self._pos, self._delete
 
            for index in indices:
                pos, idx = _pos(index)
                _delete(pos, idx)
        else:
            pos, idx = self._pos(index)
            self._delete(pos, idx)
 
    def __getitem__(self, index):
        """Lookup value at `index` in sorted list.
 
        ``sl.__getitem__(index)`` <==> ``sl[index]``
 
        Supports slicing.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList('abcde')
        >>> sl[1]
        'b'
        >>> sl[-1]
        'e'
        >>> sl[2:5]
        ['c', 'd', 'e']
 
        :param index: integer or slice for indexing
        :return: value or list of values
        :raises IndexError: if index out of range
 
        """
        _lists = self._lists
 
        if isinstance(index, slice):
            start, stop, step = index.indices(self._len)
 
            if step == 1 and start < stop:
                # Whole slice optimization: start to stop slices the whole
                # sorted list.
 
                if start == 0 and stop == self._len:
                    return reduce(iadd, self._lists, [])
 
                start_pos, start_idx = self._pos(start)
                start_list = _lists[start_pos]
                stop_idx = start_idx + stop - start
 
                # Small slice optimization: start index and stop index are
                # within the start list.
 
                if len(start_list) >= stop_idx:
                    return start_list[start_idx:stop_idx]
 
                if stop == self._len:
                    stop_pos = len(_lists) - 1
                    stop_idx = len(_lists[stop_pos])
                else:
                    stop_pos, stop_idx = self._pos(stop)
 
                prefix = _lists[start_pos][start_idx:]
                middle = _lists[(start_pos + 1) : stop_pos]
                result = reduce(iadd, middle, prefix)
                result += _lists[stop_pos][:stop_idx]
 
                return result
 
            if step == -1 and start > stop:
                result = self._getitem(slice(stop + 1, start + 1))
                result.reverse()
                return result
 
            # Return a list because a negative step could
            # reverse the order of the items and this could
            # be the desired behavior.
 
            indices = range(start, stop, step)
            return list(self._getitem(index) for index in indices)
        else:
            if self._len:
                if index == 0:
                    return _lists[0][0]
                elif index == -1:
                    return _lists[-1][-1]
            else:
                raise IndexError("list index out of range")
 
            if 0 <= index < len(_lists[0]):
                return _lists[0][index]
 
            len_last = len(_lists[-1])
 
            if -len_last < index < 0:
                return _lists[-1][len_last + index]
 
            pos, idx = self._pos(index)
            return _lists[pos][idx]
 
    _getitem = __getitem__
 
    def __setitem__(self, index, value):
        """Raise not-implemented error.
 
        ``sl.__setitem__(index, value)`` <==> ``sl[index] = value``
 
        :raises NotImplementedError: use ``del sl[index]`` and
            ``sl.add(value)`` instead
 
        """
        message = "use ``del sl[index]`` and ``sl.add(value)`` instead"
        raise NotImplementedError(message)
 
    def __iter__(self):
        """Return an iterator over the sorted list.
 
        ``sl.__iter__()`` <==> ``iter(sl)``
 
        Iterating the sorted list while adding or deleting values may raise a
        :exc:`RuntimeError` or fail to iterate over all values.
 
        """
        return chain.from_iterable(self._lists)
 
    def __reversed__(self):
        """Return a reverse iterator over the sorted list.
 
        ``sl.__reversed__()`` <==> ``reversed(sl)``
 
        Iterating the sorted list while adding or deleting values may raise a
        :exc:`RuntimeError` or fail to iterate over all values.
 
        """
        return chain.from_iterable(map(reversed, reversed(self._lists)))
 
    def reverse(self):
        """Raise not-implemented error.
 
        Sorted list maintains values in ascending sort order. Values may not be
        reversed in-place.
 
        Use ``reversed(sl)`` for an iterator over values in descending sort
        order.
 
        Implemented to override `MutableSequence.reverse` which provides an
        erroneous default implementation.
 
        :raises NotImplementedError: use ``reversed(sl)`` instead
 
        """
        raise NotImplementedError("use ``reversed(sl)`` instead")
 
    def islice(self, start=None, stop=None, reverse=False):
        """Return an iterator that slices sorted list from `start` to `stop`.
 
        The `start` and `stop` index are treated inclusive and exclusive,
        respectively.
 
        Both `start` and `stop` default to `None` which is automatically
        inclusive of the beginning and end of the sorted list.
 
        When `reverse` is `True` the values are yielded from the iterator in
        reverse order; `reverse` defaults to `False`.
 
        >>> sl = SortedList('abcdefghij')
        >>> it = sl.islice(2, 6)
        >>> list(it)
        ['c', 'd', 'e', 'f']
 
        :param int start: start index (inclusive)
        :param int stop: stop index (exclusive)
        :param bool reverse: yield values in reverse order
        :return: iterator
 
        """
        _len = self._len
 
        if not _len:
            return iter(())
 
        start, stop, _ = slice(start, stop).indices(self._len)
 
        if start >= stop:
            return iter(())
 
        _pos = self._pos
 
        min_pos, min_idx = _pos(start)
 
        if stop == _len:
            max_pos = len(self._lists) - 1
            max_idx = len(self._lists[-1])
        else:
            max_pos, max_idx = _pos(stop)
 
        return self._islice(min_pos, min_idx, max_pos, max_idx, reverse)
 
    def _islice(self, min_pos, min_idx, max_pos, max_idx, reverse):
        """Return an iterator that slices sorted list using two index pairs.
 
        The index pairs are (min_pos, min_idx) and (max_pos, max_idx), the
        first inclusive and the latter exclusive. See `_pos` for details on how
        an index is converted to an index pair.
 
        When `reverse` is `True`, values are yielded from the iterator in
        reverse order.
 
        """
        _lists = self._lists
 
        if min_pos > max_pos:
            return iter(())
 
        if min_pos == max_pos:
            if reverse:
                indices = reversed(range(min_idx, max_idx))
                return map(_lists[min_pos].__getitem__, indices)
 
            indices = range(min_idx, max_idx)
            return map(_lists[min_pos].__getitem__, indices)
 
        next_pos = min_pos + 1
 
        if next_pos == max_pos:
            if reverse:
                min_indices = range(min_idx, len(_lists[min_pos]))
                max_indices = range(max_idx)
                return chain(
                    map(_lists[max_pos].__getitem__, reversed(max_indices)),
                    map(_lists[min_pos].__getitem__, reversed(min_indices)),
                )
 
            min_indices = range(min_idx, len(_lists[min_pos]))
            max_indices = range(max_idx)
            return chain(
                map(_lists[min_pos].__getitem__, min_indices),
                map(_lists[max_pos].__getitem__, max_indices),
            )
 
        if reverse:
            min_indices = range(min_idx, len(_lists[min_pos]))
            sublist_indices = range(next_pos, max_pos)
            sublists = map(_lists.__getitem__, reversed(sublist_indices))
            max_indices = range(max_idx)
            return chain(
                map(_lists[max_pos].__getitem__, reversed(max_indices)),
                chain.from_iterable(map(reversed, sublists)),
                map(_lists[min_pos].__getitem__, reversed(min_indices)),
            )
 
        min_indices = range(min_idx, len(_lists[min_pos]))
        sublist_indices = range(next_pos, max_pos)
        sublists = map(_lists.__getitem__, sublist_indices)
        max_indices = range(max_idx)
        return chain(
            map(_lists[min_pos].__getitem__, min_indices),
            chain.from_iterable(sublists),
            map(_lists[max_pos].__getitem__, max_indices),
        )
 
    def irange(self, minimum=None, maximum=None, inclusive=(True, True), reverse=False):
        """Create an iterator of values between `minimum` and `maximum`.
 
        Both `minimum` and `maximum` default to `None` which is automatically
        inclusive of the beginning and end of the sorted list.
 
        The argument `inclusive` is a pair of booleans that indicates whether
        the minimum and maximum ought to be included in the range,
        respectively. The default is ``(True, True)`` such that the range is
        inclusive of both minimum and maximum.
 
        When `reverse` is `True` the values are yielded from the iterator in
        reverse order; `reverse` defaults to `False`.
 
        >>> sl = SortedList('abcdefghij')
        >>> it = sl.irange('c', 'f')
        >>> list(it)
        ['c', 'd', 'e', 'f']
 
        :param minimum: minimum value to start iterating
        :param maximum: maximum value to stop iterating
        :param inclusive: pair of booleans
        :param bool reverse: yield values in reverse order
        :return: iterator
 
        """
        _maxes = self._maxes
 
        if not _maxes:
            return iter(())
 
        _lists = self._lists
 
        # Calculate the minimum (pos, idx) pair. By default this location
        # will be inclusive in our calculation.
 
        if minimum is None:
            min_pos = 0
            min_idx = 0
        else:
            if inclusive[0]:
                min_pos = bisect_left(_maxes, minimum)
 
                if min_pos == len(_maxes):
                    return iter(())
 
                min_idx = bisect_left(_lists[min_pos], minimum)
            else:
                min_pos = bisect_right(_maxes, minimum)
 
                if min_pos == len(_maxes):
                    return iter(())
 
                min_idx = bisect_right(_lists[min_pos], minimum)
 
        # Calculate the maximum (pos, idx) pair. By default this location
        # will be exclusive in our calculation.
 
        if maximum is None:
            max_pos = len(_maxes) - 1
            max_idx = len(_lists[max_pos])
        else:
            if inclusive[1]:
                max_pos = bisect_right(_maxes, maximum)
 
                if max_pos == len(_maxes):
                    max_pos -= 1
                    max_idx = len(_lists[max_pos])
                else:
                    max_idx = bisect_right(_lists[max_pos], maximum)
            else:
                max_pos = bisect_left(_maxes, maximum)
 
                if max_pos == len(_maxes):
                    max_pos -= 1
                    max_idx = len(_lists[max_pos])
                else:
                    max_idx = bisect_left(_lists[max_pos], maximum)
 
        return self._islice(min_pos, min_idx, max_pos, max_idx, reverse)
 
    def __len__(self):
        """Return the size of the sorted list.
 
        ``sl.__len__()`` <==> ``len(sl)``
 
        :return: size of sorted list
 
        """
        return self._len
 
    def bisect_left(self, value):
        """Return an index to insert `value` in the sorted list.
 
        If the `value` is already present, the insertion point will be before
        (to the left of) any existing values.
 
        Similar to the `bisect` module in the standard library.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([10, 11, 12, 13, 14])
        >>> sl.bisect_left(12)
        2
 
        :param value: insertion index of value in sorted list
        :return: index
 
        """
        _maxes = self._maxes
 
        if not _maxes:
            return 0
 
        pos = bisect_left(_maxes, value)
 
        if pos == len(_maxes):
            return self._len
 
        idx = bisect_left(self._lists[pos], value)
        return self._loc(pos, idx)
 
    def bisect_right(self, value):
        """Return an index to insert `value` in the sorted list.
 
        Similar to `bisect_left`, but if `value` is already present, the
        insertion point will be after (to the right of) any existing values.
 
        Similar to the `bisect` module in the standard library.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([10, 11, 12, 13, 14])
        >>> sl.bisect_right(12)
        3
 
        :param value: insertion index of value in sorted list
        :return: index
 
        """
        _maxes = self._maxes
 
        if not _maxes:
            return 0
 
        pos = bisect_right(_maxes, value)
 
        if pos == len(_maxes):
            return self._len
 
        idx = bisect_right(self._lists[pos], value)
        return self._loc(pos, idx)
 
    bisect = bisect_right
    _bisect_right = bisect_right
 
    def count(self, value):
        """Return number of occurrences of `value` in the sorted list.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
        >>> sl.count(3)
        3
 
        :param value: value to count in sorted list
        :return: count
 
        """
        _maxes = self._maxes
 
        if not _maxes:
            return 0
 
        pos_left = bisect_left(_maxes, value)
 
        if pos_left == len(_maxes):
            return 0
 
        _lists = self._lists
        idx_left = bisect_left(_lists[pos_left], value)
        pos_right = bisect_right(_maxes, value)
 
        if pos_right == len(_maxes):
            return self._len - self._loc(pos_left, idx_left)
 
        idx_right = bisect_right(_lists[pos_right], value)
 
        if pos_left == pos_right:
            return idx_right - idx_left
 
        right = self._loc(pos_right, idx_right)
        left = self._loc(pos_left, idx_left)
        return right - left
 
    def copy(self):
        """Return a shallow copy of the sorted list.
 
        Runtime complexity: `O(n)`
 
        :return: new sorted list
 
        """
        return self.__class__(self)
 
    __copy__ = copy
 
    def append(self, value):
        """Raise not-implemented error.
 
        Implemented to override `MutableSequence.append` which provides an
        erroneous default implementation.
 
        :raises NotImplementedError: use ``sl.add(value)`` instead
 
        """
        raise NotImplementedError("use ``sl.add(value)`` instead")
 
    def extend(self, values):
        """Raise not-implemented error.
 
        Implemented to override `MutableSequence.extend` which provides an
        erroneous default implementation.
 
        :raises NotImplementedError: use ``sl.update(values)`` instead
 
        """
        raise NotImplementedError("use ``sl.update(values)`` instead")
 
    def insert(self, index, value):
        """Raise not-implemented error.
 
        :raises NotImplementedError: use ``sl.add(value)`` instead
 
        """
        raise NotImplementedError("use ``sl.add(value)`` instead")
 
    def pop(self, index=-1):
        """Remove and return value at `index` in sorted list.
 
        Raise :exc:`IndexError` if the sorted list is empty or index is out of
        range.
 
        Negative indices are supported.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList('abcde')
        >>> sl.pop()
        'e'
        >>> sl.pop(2)
        'c'
        >>> sl
        SortedList(['a', 'b', 'd'])
 
        :param int index: index of value (default -1)
        :return: value
        :raises IndexError: if index is out of range
 
        """
        if not self._len:
            raise IndexError("pop index out of range")
 
        _lists = self._lists
 
        if index == 0:
            val = _lists[0][0]
            self._delete(0, 0)
            return val
 
        if index == -1:
            pos = len(_lists) - 1
            loc = len(_lists[pos]) - 1
            val = _lists[pos][loc]
            self._delete(pos, loc)
            return val
 
        if 0 <= index < len(_lists[0]):
            val = _lists[0][index]
            self._delete(0, index)
            return val
 
        len_last = len(_lists[-1])
 
        if -len_last < index < 0:
            pos = len(_lists) - 1
            loc = len_last + index
            val = _lists[pos][loc]
            self._delete(pos, loc)
            return val
 
        pos, idx = self._pos(index)
        val = _lists[pos][idx]
        self._delete(pos, idx)
        return val
 
    def index(self, value, start=None, stop=None):
        """Return first index of value in sorted list.
 
        Raise ValueError if `value` is not present.
 
        Index must be between `start` and `stop` for the `value` to be
        considered present. The default value, None, for `start` and `stop`
        indicate the beginning and end of the sorted list.
 
        Negative indices are supported.
 
        Runtime complexity: `O(log(n))` -- approximate.
 
        >>> sl = SortedList('abcde')
        >>> sl.index('d')
        3
        >>> sl.index('z')
        Traceback (most recent call last):
          ...
        ValueError: 'z' is not in list
 
        :param value: value in sorted list
        :param int start: start index (default None, start of sorted list)
        :param int stop: stop index (default None, end of sorted list)
        :return: index of value
        :raises ValueError: if value is not present
 
        """
        _len = self._len
 
        if not _len:
            raise ValueError("{0!r} is not in list".format(value))
 
        if start is None:
            start = 0
        if start < 0:
            start += _len
        if start < 0:
            start = 0
 
        if stop is None:
            stop = _len
        if stop < 0:
            stop += _len
        if stop > _len:
            stop = _len
 
        if stop <= start:
            raise ValueError("{0!r} is not in list".format(value))
 
        _maxes = self._maxes
        pos_left = bisect_left(_maxes, value)
 
        if pos_left == len(_maxes):
            raise ValueError("{0!r} is not in list".format(value))
 
        _lists = self._lists
        idx_left = bisect_left(_lists[pos_left], value)
 
        if _lists[pos_left][idx_left] != value:
            raise ValueError("{0!r} is not in list".format(value))
 
        stop -= 1
        left = self._loc(pos_left, idx_left)
 
        if start <= left:
            if left <= stop:
                return left
        else:
            right = self._bisect_right(value) - 1
 
            if start <= right:
                return start
 
        raise ValueError("{0!r} is not in list".format(value))
 
    def __add__(self, other):
        """Return new sorted list containing all values in both sequences.
 
        ``sl.__add__(other)`` <==> ``sl + other``
 
        Values in `other` do not need to be in sorted order.
 
        Runtime complexity: `O(n*log(n))`
 
        >>> sl1 = SortedList('bat')
        >>> sl2 = SortedList('cat')
        >>> sl1 + sl2
        SortedList(['a', 'a', 'b', 'c', 't', 't'])
 
        :param other: other iterable
        :return: new sorted list
 
        """
        values = reduce(iadd, self._lists, [])
        values.extend(other)
        return self.__class__(values)
 
    __radd__ = __add__
 
    def __iadd__(self, other):
        """Update sorted list with values from `other`.
 
        ``sl.__iadd__(other)`` <==> ``sl += other``
 
        Values in `other` do not need to be in sorted order.
 
        Runtime complexity: `O(k*log(n))` -- approximate.
 
        >>> sl = SortedList('bat')
        >>> sl += 'cat'
        >>> sl
        SortedList(['a', 'a', 'b', 'c', 't', 't'])
 
        :param other: other iterable
        :return: existing sorted list
 
        """
        self._update(other)
        return self
 
    def __mul__(self, num):
        """Return new sorted list with `num` shallow copies of values.
 
        ``sl.__mul__(num)`` <==> ``sl * num``
 
        Runtime complexity: `O(n*log(n))`
 
        >>> sl = SortedList('abc')
        >>> sl * 3
        SortedList(['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'])
 
        :param int num: count of shallow copies
        :return: new sorted list
 
        """
        values = reduce(iadd, self._lists, []) * num
        return self.__class__(values)
 
    __rmul__ = __mul__
 
    def __imul__(self, num):
        """Update the sorted list with `num` shallow copies of values.
 
        ``sl.__imul__(num)`` <==> ``sl *= num``
 
        Runtime complexity: `O(n*log(n))`
 
        >>> sl = SortedList('abc')
        >>> sl *= 3
        >>> sl
        SortedList(['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'])
 
        :param int num: count of shallow copies
        :return: existing sorted list
 
        """
        values = reduce(iadd, self._lists, []) * num
        self._clear()
        self._update(values)
        return self
 
    def __make_cmp(seq_op, symbol, doc):
        "Make comparator method."
 
        def comparer(self, other):
            "Compare method for sorted list and sequence."
            if not isinstance(other, Sequence):
                return NotImplemented
 
            self_len = self._len
            len_other = len(other)
 
            if self_len != len_other:
                if seq_op is eq:
                    return False
                if seq_op is ne:
                    return True
 
            for alpha, beta in zip(self, other):
                if alpha != beta:
                    return seq_op(alpha, beta)
 
            return seq_op(self_len, len_other)
 
        seq_op_name = seq_op.__name__
        comparer.__name__ = "__{0}__".format(seq_op_name)
        doc_str = """Return true if and only if sorted list is {0} `other`.
 
        ``sl.__{1}__(other)`` <==> ``sl {2} other``
 
        Comparisons use lexicographical order as with sequences.
 
        Runtime complexity: `O(n)`
 
        :param other: `other` sequence
        :return: true if sorted list is {0} `other`
 
        """
        comparer.__doc__ = dedent(doc_str.format(doc, seq_op_name, symbol))
        return comparer
 
    __eq__ = __make_cmp(eq, "==", "equal to")
    __ne__ = __make_cmp(ne, "!=", "not equal to")
    __lt__ = __make_cmp(lt, "<", "less than")
    __gt__ = __make_cmp(gt, ">", "greater than")
    __le__ = __make_cmp(le, "<=", "less than or equal to")
    __ge__ = __make_cmp(ge, ">=", "greater than or equal to")
 
    __make_cmp = staticmethod(__make_cmp)
 
    def __reduce__(self):
        values = reduce(iadd, self._lists, [])
        return (type(self), (values,))
 
    def _check(self):
        """Check invariants of sorted list.
 
        Runtime complexity: `O(n)`
 
        """
        try:
            assert self._load >= 4
            assert len(self._maxes) == len(self._lists)
            assert self._len == sum(len(sublist) for sublist in self._lists)
 
            # Check all sublists are sorted.
 
            for sublist in self._lists:
                for pos in range(1, len(sublist)):
                    assert sublist[pos - 1] <= sublist[pos]
 
            # Check beginning/end of sublists are sorted.
 
            for pos in range(1, len(self._lists)):
                assert self._lists[pos - 1][-1] <= self._lists[pos][0]
 
            # Check _maxes index is the last value of each sublist.
 
            for pos in range(len(self._maxes)):
                assert self._maxes[pos] == self._lists[pos][-1]
 
            # Check sublist lengths are less than double load-factor.
 
            double = self._load << 1
            assert all(len(sublist) <= double for sublist in self._lists)
 
            # Check sublist lengths are greater than half load-factor for all
            # but the last sublist.
 
            half = self._load >> 1
            for pos in range(0, len(self._lists) - 1):
                assert len(self._lists[pos]) >= half
 
            if self._index:
                assert self._len == self._index[0]
                assert len(self._index) == self._offset + len(self._lists)
 
                # Check index leaf nodes equal length of sublists.
 
                for pos in range(len(self._lists)):
                    leaf = self._index[self._offset + pos]
                    assert leaf == len(self._lists[pos])
 
                # Check index branch nodes are the sum of their children.
 
                for pos in range(self._offset):
                    child = (pos << 1) + 1
                    if child >= len(self._index):
                        assert self._index[pos] == 0
                    elif child + 1 == len(self._index):
                        assert self._index[pos] == self._index[child]
                    else:
                        child_sum = self._index[child] + self._index[child + 1]
                        assert child_sum == self._index[pos]
        except:
            traceback.print_exc(file=sys.stdout)
            print("len", self._len)
            print("load", self._load)
            print("offset", self._offset)
            print("len_index", len(self._index))
            print("index", self._index)
            print("len_maxes", len(self._maxes))
            print("maxes", self._maxes)
            print("len_lists", len(self._lists))
            print("lists", self._lists)
            raise
 
 
############### END OF SORTED LIST TEMPLATE ########################

input()
heights = list(map(int, input().split()))

#from cases import heights  

Stopwatch.hide = True
a = Stopwatch("height_time")
b = Stopwatch("find_children")
c = Stopwatch("total")
d = Stopwatch("bisect")
e = Stopwatch("pop")

c.start_cumulative()

def build_height_map(heights):
    map = {}
    highest_balloon = heights[0]

    for index, height in enumerate(heights):
        if height > highest_balloon:
            highest_balloon = height

        if map.get(height):
            map[height].append(index)
            continue
        map[height] = [index]

    for layerKey in map:
        map[layerKey] = SortedList(map[layerKey])

    return map, highest_balloon

arrows = 0

a.start_cumulative()
heights_map, start_height = build_height_map(heights)
a.stop_cumulative()
a.print()
SortedList.pop
current_height = start_height
while current_height != 0:

    height_layer = heights_map.get(current_height)

    if not height_layer:
        current_height -= 1
        continue

    index = height_layer.pop(0)
    arrows += 1

    start_height = current_height
    start_height -= 1
    might_pop_list = heights_map.get(start_height)

    b.start_cumulative()
    while might_pop_list:

        d.start_cumulative()
        index_index = might_pop_list.bisect_left(index)
        d.stop_cumulative()

        if index_index == len(might_pop_list):
            break
        might_pop_index = might_pop_list[index_index]

        e.start_cumulative()
        might_pop_list.pop(index_index)
        e.stop_cumulative()

        start_height -= 1
        might_pop_list = heights_map.get(start_height)
        index = might_pop_index

    b.stop_cumulative()

b.print()

print(arrows)

c.stop_cumulative()
d.print()
c.print()
e.print()

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 22ms
memory: 11324kb

input:

5
3 2 1 5 4

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 26ms
memory: 11324kb

input:

4
1 2 3 4

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 11ms
memory: 11396kb

input:

6
5 3 2 4 6 1

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 16ms
memory: 11328kb

input:

7
2 4 3 10 10 1 3

output:

5

result:

ok single line: '5'

Test #5:

score: 0
Accepted
time: 13ms
memory: 11364kb

input:

66
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200

output:

66

result:

ok single line: '66'

Test #6:

score: 0
Accepted
time: 4ms
memory: 11340kb

input:

92
25 29 24 26 21 29 20 24 26 24 29 27 22 24 24 25 21 26 26 29 24 27 30 22 20 28 25 22 20 24 30 27 23 21 25 23 30 24 22 28 22 29 24 30 26 29 21 20 21 23 23 22 28 28 27 30 28 29 29 23 24 23 29 30 21 25 25 24 26 30 23 30 27 27 21 30 29 21 30 29 24 27 24 20 26 25 29 30 23 21 27 21

output:

37

result:

ok single line: '37'

Test #7:

score: 0
Accepted
time: 10ms
memory: 11344kb

input:

34
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

output:

34

result:

ok single line: '34'

Test #8:

score: 0
Accepted
time: 12ms
memory: 11320kb

input:

11
100 99 98 97 96 95 94 93 92 91 90

output:

1

result:

ok single line: '1'

Test #9:

score: 0
Accepted
time: 15ms
memory: 11420kb

input:

92
90 51 99 82 72 73 67 93 81 67 53 70 70 86 92 95 68 52 68 78 85 98 62 53 70 93 64 91 82 65 90 81 99 83 87 65 71 90 57 80 67 50 72 93 58 95 53 76 65 76 91 63 51 66 72 66 68 83 64 65 52 60 78 77 80 90 59 77 92 90 93 54 60 52 74 50 53 53 55 60 59 58 61 75 70 83 76 96 51 67 86 74

output:

53

result:

ok single line: '53'

Test #10:

score: 0
Accepted
time: 16ms
memory: 11336kb

input:

61
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200

output:

61

result:

ok single line: '61'

Test #11:

score: 0
Accepted
time: 13ms
memory: 11548kb

input:

966
99 15 11 38 81 47 82 74 80 41 100 34 64 35 82 15 45 45 18 20 2 35 88 68 24 3 14 80 83 93 40 71 76 70 20 96 11 67 60 37 87 76 33 68 67 38 14 5 75 86 41 61 99 72 38 98 18 28 47 13 68 85 86 80 19 62 5 65 80 49 16 7 98 75 82 15 39 42 10 40 94 15 86 89 99 83 95 55 86 91 28 69 7 39 34 94 14 26 30 11 2...

output:

305

result:

ok single line: '305'

Test #12:

score: 0
Accepted
time: 21ms
memory: 11452kb

input:

318
1608 567 852 870 1741 1746 1550 797 202 1090 422 658 1622 1661 426 1635 254 497 1207 570 1673 1314 46 1770 1236 28 791 419 1383 754 518 1172 182 268 394 246 422 1429 58 1646 701 883 455 615 694 827 539 1675 1044 682 952 397 1131 384 62 1851 1728 683 1431 880 866 1885 896 97 731 674 805 1663 208 ...

output:

288

result:

ok single line: '288'

Test #13:

score: 0
Accepted
time: 18ms
memory: 11812kb

input:

975
2452 5428 8720 1930 8751 4758 1353 8824 1630 5648 1025 7764 2884 8739 2088 85 5227 5802 8988 8690 7216 4947 4045 3261 1732 9925 293 5867 5340 5749 1710 2295 2756 4787 3971 5806 6677 6117 2180 8352 8762 1593 8461 5088 1537 3845 2322 5859 5234 423 7624 6071 354 8411 7066 6012 7579 5511 7007 7610 5...

output:

937

result:

ok single line: '937'

Test #14:

score: 0
Accepted
time: 14ms
memory: 11424kb

input:

722
10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10...

output:

722

result:

ok single line: '722'

Test #15:

score: 0
Accepted
time: 12ms
memory: 11420kb

input:

265
2000 1999 1998 1997 1996 1995 1994 1993 1992 1991 1990 1989 1988 1987 1986 1985 1984 1983 1982 1981 1980 1979 1978 1977 1976 1975 1974 1973 1972 1971 1970 1969 1968 1967 1966 1965 1964 1963 1962 1961 1960 1959 1958 1957 1956 1955 1954 1953 1952 1951 1950 1949 1948 1947 1946 1945 1944 1943 1942 1...

output:

1

result:

ok single line: '1'

Test #16:

score: 0
Accepted
time: 16ms
memory: 11684kb

input:

641
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 ...

output:

641

result:

ok single line: '641'

Test #17:

score: 0
Accepted
time: 17ms
memory: 11440kb

input:

303
2000 1 1999 2 1998 3 1997 4 1996 5 1995 6 1994 7 1993 8 1992 9 1991 10 1990 11 1989 12 1988 13 1987 14 1986 15 1985 16 1984 17 1983 18 1982 19 1981 20 1980 21 1979 22 1978 23 1977 24 1976 25 1975 26 1974 27 1973 28 1972 29 1971 30 1970 31 1969 32 1968 33 1967 34 1966 35 1965 36 1964 37 1963 38 1...

output:

152

result:

ok single line: '152'

Test #18:

score: 0
Accepted
time: 15ms
memory: 12396kb

input:

1982
495 2468 707 2838 4124 28 1208 1223 3885 4149 2320 2468 4818 326 3426 4380 978 1783 4538 3876 218 3015 3135 498 1716 2381 206 1717 4942 2983 3622 846 2434 1812 4093 817 2706 2360 12 3070 1074 4330 1518 3002 689 354 3083 4259 1546 2497 1250 345 3052 3778 2402 458 2895 2210 643 1153 3844 1122 214...

output:

1667

result:

ok single line: '1667'

Test #19:

score: 0
Accepted
time: 25ms
memory: 12340kb

input:

1692
754 643 95 1335 4297 304 2299 4994 562 1729 4151 4197 580 2895 3760 2569 2023 947 3741 4035 2845 1539 4778 2842 4954 576 4547 2028 2840 3130 902 3990 1051 3893 221 1032 2963 107 4397 3297 2243 685 1721 861 4588 1565 1676 3671 4805 161 1192 1092 620 4380 845 4478 4482 4837 2996 1075 512 172 4735...

output:

1456

result:

ok single line: '1456'

Test #20:

score: 0
Accepted
time: 13ms
memory: 11960kb

input:

1195
2942 2042 4111 4751 1100 2367 26 2094 4745 2332 97 4622 1727 4696 4146 4386 4026 561 2742 747 3085 720 4047 3005 524 3322 1673 3288 4322 787 320 3529 4984 3529 4235 3575 867 4999 1649 3859 2809 3942 4117 244 640 1262 1557 1737 1365 48 3797 4720 2783 4914 4435 3529 4053 4319 1546 4617 3376 4664 ...

output:

1057

result:

ok single line: '1057'

Test #21:

score: 0
Accepted
time: 62ms
memory: 19328kb

input:

14255
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 10...

output:

14255

result:

ok single line: '14255'

Test #22:

score: 0
Accepted
time: 56ms
memory: 19356kb

input:

14370
50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49986 49985 49984 49983 49982 49981 49980 49979 49978 49977 49976 49975 49974 49973 49972 49971 49970 49969 49968 49967 49966 49965 49964 49963 49962 49961 49960 49959 49958 49957 49956 49955 49954 49953 49952 ...

output:

1

result:

ok single line: '1'

Test #23:

score: 0
Accepted
time: 34ms
memory: 12556kb

input:

18877
200 1 199 2 198 3 197 4 196 5 195 6 194 7 193 8 192 9 191 10 190 11 189 12 188 13 187 14 186 15 185 16 184 17 183 18 182 19 181 20 180 21 179 22 178 23 177 24 176 25 175 26 174 27 173 28 172 29 171 30 170 31 169 32 168 33 167 34 166 35 165 36 164 37 163 38 162 39 161 40 160 41 159 42 158 43 15...

output:

294

result:

ok single line: '294'

Test #24:

score: 0
Accepted
time: 63ms
memory: 19052kb

input:

14832
2439 7481 16966 10795 10763 29802 2932 8507 8612 16320 6817 10678 19157 12056 15975 13379 6960 49885 9174 48390 30870 25364 4490 24275 22179 42519 44632 37174 22168 41839 23843 34574 4820 13245 24774 37816 6411 30203 24087 40088 8664 37136 21867 14740 13237 4699 29260 6483 36398 16389 25370 26...

output:

12964

result:

ok single line: '12964'

Test #25:

score: 0
Accepted
time: 707ms
memory: 78136kb

input:

128564
43174 101316 145021 235834 266929 158282 221074 385874 419875 40333 356029 152198 118545 164943 298788 288027 110097 69388 419627 234448 455843 406257 362322 145712 86934 139037 410879 484936 299376 468 354224 110358 399967 10140 411711 17151 23708 408943 276357 417155 99211 33639 150333 2140...

output:

114347

result:

ok single line: '114347'

Test #26:

score: 0
Accepted
time: 370ms
memory: 17540kb

input:

124965
9 7 8 5 10 10 8 8 7 7 8 9 6 6 6 8 8 9 7 9 8 9 7 9 9 7 7 9 6 5 8 10 8 7 6 5 5 7 7 7 5 5 10 8 6 6 9 8 10 8 7 5 7 7 10 8 5 6 7 7 9 9 5 8 8 6 9 7 7 8 5 8 6 10 5 6 6 7 8 5 5 7 6 10 7 7 5 9 8 6 6 7 10 6 9 7 8 7 7 7 5 5 5 7 9 10 6 9 9 7 7 9 6 8 5 8 5 9 10 10 8 7 9 8 6 7 7 10 10 8 7 9 5 9 5 6 9 5 6 5...

output:

21345

result:

ok single line: '21345'

Test #27:

score: 0
Accepted
time: 839ms
memory: 88132kb

input:

149568
392456 194364 473586 341623 85085 449856 42257 6694 225148 64414 343268 363860 270685 137030 258048 349950 154805 337626 107628 244711 245139 386344 402249 459757 18458 477114 164503 219356 429664 1987 494047 438958 394997 312227 394092 98607 371312 179540 446183 216929 232172 270338 358841 4...

output:

131116

result:

ok single line: '131116'

Test #28:

score: -100
Time Limit Exceeded

input:

500000
70482 203993 218067 351194 155614 346400 271367 132298 30183 240460 372599 175370 143196 309039 369855 38135 366319 49557 46962 341420 282340 496544 264656 468523 414310 205085 438162 449960 26385 195991 174138 93920 267535 14861 4037 159275 276239 361056 439284 165480 388971 271788 134079 32...

output:


result: