Playing Piano
Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence $$$a_1, a_2, \ldots, a_n$$$ of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.Let's denote the fingers of hand by numbers from $$$1$$$ to $$$5$$$. We call a fingering any sequence $$$b_1, \ldots, b_n$$$ of fingers numbers. A fingering is convenient if for all $$$1\leq i \leq n - 1$$$ the following holds:  if $$$a_i < a_{i+1}$$$ then $$$b_i < b_{i+1}$$$, because otherwise Paul needs to take his hand off the keyboard to play the $$$(i+1)$$$-st note;  if $$$a_i > a_{i+1}$$$ then $$$b_i > b_{i+1}$$$, because of the same;  if $$$a_i = a_{i+1}$$$ then $$$b_i\neq b_{i+1}$$$, because using the same finger twice in a row is dumb. Please note that there is $$$\neq$$$, not $$$=$$$ between $$$b_i$$$ and $$$b_{i+1}$$$. Please provide any convenient fingering or find out that there is none.
The third sample test is kinda "Non stop" song by Reflex.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the number of notes.The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$) denoting the positions of notes on the keyboard.
If there is no convenient fingering, print $$$-1$$$. Otherwise, print $$$n$$$ numbers $$$b_1, b_2, \ldots, b_n$$$, each from $$$1$$$ to $$$5$$$, denoting a convenient fingering, separated by spaces.
import sys
input = sys.stdin.readline
n = int(input())
L = list(map(int, input().split()))
ans = []
if n == 1:
    print(3)
else:
    c = 0
    if L[0] == L[1]:
        ans.append(3)
    elif L[0] < L[1]:
        ans.append(1)
    else:
        ans.append(5)
    for i in range(n - 1):
        if L[i + 1] > L[i] and ans[i] == 5:
            print(-1)
            c = 1
            break
        if L[i + 1] < L[i] and ans[i] == 1:
            print(-1)
            c = 1
            break
        if L[i + 1] > L[i]:
            cur = ans[i] + 1
            if i < n - 2:
                if L[i + 2] < L[i + 1]:
                    cur = 5
        elif L[i + 1] < L[i]:
            cur = ans[i] - 1
            if i < n - 2:
                if L[i + 2] > L[i + 1]:
                    cur = 1
        else:
            if ans[i] == 3:
                cur = 2
            else:
                cur = 3
            if i < n - 2:
                if L[i + 2] > L[i + 1]:
                    cur = 2 if ans[i] == 1 else 1
                if L[i + 2] < L[i + 1]:
                    cur = 4 if ans[i] == 5 else 5
        ans.append(cur)
    if c == 0:
        print(*ans)
import collections

class seq:

    def __init__(self, note_amount, sign):
        self.note_amount = note_amount
        self.sign = sign

    def take_sign(self):
        return self.sign

    def take_note_amount(self):
        return self.note_amount

    def append_note(self, amount):
        self.note_amount += 1

def resolve_seq(seqs, flags, tmp, sign):
    if flags[sign]:
        tmp.append_note(1)
    else:
        flags[tmp.take_sign()] = False
        flags[sign] = True
        seqs.append(tmp)
        tmp = seq(1, sign)
    return tmp

def applicatura(n, notes):
    appl = []
    seqs = sequence(n, notes)
    if len(seqs) == 0:
        return [1]
    if seqs[0].take_sign() == 2:
        appl.append(1)
    elif seqs[0].take_sign() == 1:
        appl.append(3)
    else:
        appl.append(5)
    lens = len(seqs)
    for seqn in range(lens):
        x = appl[-1]
        if seqn <= lens - 2:
            sign = seqs[seqn + 1].take_sign()
        else:
            sign = 1
        if seqs[seqn].take_sign() == 0:
            if sign == 2:
                for i in range(x - 1, x - 1 - (seqs[seqn].take_note_amount() - 1), -1):
                    if i < 1:
                        return [-1]
                    appl.append(i)
                appl.append(1)
            else:
                for i in range(x - 1, x - 1 - seqs[seqn].take_note_amount(), -1):
                    if i < 1:
                        return [-1]
                    appl.append(i)
        if seqs[seqn].take_sign() == 2:
            if sign == 0:
                for i in range(x + 1, x + 1 + (seqs[seqn].take_note_amount() - 1)):
                    if i > 5:
                        return [-1]
                    appl.append(i)
                appl.append(5)
            else:
                for i in range(x + 1, x + 1 + seqs[seqn].take_note_amount()):
                    if i > 5:
                        return [-1]
                    appl.append(i)
        if seqs[seqn].take_sign() == 1:
            for i in range(seqs[seqn].take_note_amount() - 1):
                if x != 3:
                    appl.append(3)
                    x = 3
                else:
                    appl.append(4)
                    x = 4
            if sign == 0:
                if x == 5:
                    appl.append(4)
                else:
                    appl.append(5)
            if sign == 1:
                if x != 3:
                    appl.append(3)
                    x = 3
                else:
                    appl.append(4)
                    x = 4
            if sign == 2:
                if x == 1:
                    appl.append(2)
                else:
                    appl.append(1)
    return appl

def sequence(n, notes):
    seqs = []
    flags = [False, False, False]
    tmp = seq(0, 1)
    for i in range(1, n):
        if notes[i - 1] < notes[i]:
            tmp = resolve_seq(seqs, flags, tmp, 2)
        if notes[i - 1] > notes[i]:
            tmp = resolve_seq(seqs, flags, tmp, 0)
        if notes[i - 1] == notes[i]:
            tmp = resolve_seq(seqs, flags, tmp, 1)
    seqs.append(tmp)
    seqs = seqs[1:]
    return seqs
if __name__ == '__main__':
    n = int(input())
    notes = list(map(int, input().split()))
    applic = applicatura(n, notes)
    print(*applic)
def get_fingering(notes):
    fingering = []
    diff = 0
    next_diff = None
    finger = 0
    for i in range(len(notes) - 1):
        next_diff = notes[i + 1] - notes[i]
        if diff == 0:
            if next_diff > 0:
                finger = 1 + (finger == 1)
            elif next_diff < 0:
                finger = 5 - (finger == 5)
            else:
                finger = 3 + (finger == 3)
        elif diff > 0:
            if finger == 5:
                return None
            if next_diff < 0:
                finger = 5
            else:
                finger += 1
        else:
            if finger == 1:
                return None
            if next_diff > 0:
                finger = 1
            else:
                finger -= 1
        fingering.append(finger)
        diff = next_diff
    return fingering
_ = input()
notes = [int(x) for x in input().split()]
notes.append(notes[-1])
fingering = get_fingering(notes)
if fingering:
    print(*fingering)
else:
    print(-1)