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 math

def main():
    buf = input()
    n = int(buf)
    buf = input()
    buflist = buf.split()
    a = list(map(int, buflist))
    if n == 1:
        print(1)
        return
    elif n == 2:
        if a[0] < a[1]:
            print(1, 5)
        else:
            print(5, 1)
        return
    else:
        finger_list = []
        if a[0] < a[1]:
            finger_list.append(1)
        elif a[0] > a[1]:
            finger_list.append(5)
        else:
            finger_list.append(3)
        for i in range(n - 2):
            if a[i] < a[i + 1]:
                if a[i + 1] < a[i + 2]:
                    finger_list.append(finger_list[-1] + 1)
                    if finger_list[-1] >= 6:
                        print(-1)
                        return
                elif a[i + 1] > a[i + 2]:
                    finger_list.append(5)
                    if finger_list[-2] == finger_list[-1]:
                        print(-1)
                        return
                else:
                    finger_list.append(finger_list[-1] + 1)
                    if finger_list[-1] >= 6:
                        print(-1)
                        return
            elif a[i] > a[i + 1]:
                if a[i + 1] > a[i + 2]:
                    finger_list.append(finger_list[-1] - 1)
                    if finger_list[-1] <= 0:
                        print(-1)
                        return
                elif a[i + 1] < a[i + 2]:
                    finger_list.append(1)
                    if finger_list[-2] == finger_list[-1]:
                        print(-1)
                        return
                else:
                    finger_list.append(finger_list[-1] - 1)
                    if finger_list[-1] <= 0:
                        print(-1)
                        return
            elif a[i + 1] < a[i + 2]:
                if finger_list[-1] == 1:
                    finger_list.append(2)
                else:
                    finger_list.append(1)
            elif a[i + 1] > a[i + 2]:
                if finger_list[-1] == 5:
                    finger_list.append(4)
                else:
                    finger_list.append(5)
            elif finger_list[-1] == 3:
                finger_list.append(2)
            else:
                finger_list.append(3)
        if a[-2] < a[-1]:
            finger_list.append(finger_list[-1] + 1)
            if finger_list[-1] >= 6:
                print(-1)
                return
        elif a[-2] > a[-1]:
            finger_list.append(finger_list[-1] - 1)
            if finger_list[-1] <= 0:
                print(-1)
                return
        elif finger_list[-1] == 3:
            finger_list.append(2)
        else:
            finger_list.append(3)
    p = ''
    for (i, e) in enumerate(finger_list):
        p += str(e)
        if i != len(finger_list) - 1:
            p += ' '
    print(p)
if __name__ == '__main__':
    main()
n = int(input())
note = [int(i) for i in input().split()]
part = [0 for i in range(len(note))]
if len(note) == 1:
    print(1)
else:
    if note[0] > note[1]:
        part[0] = 5
    if note[0] < note[1]:
        part[0] = 1
    if note[0] == note[1]:
        part[0] = 3
    done = True
    for i in range(1, len(note) - 1):
        if note[i] > note[i - 1]:
            if note[i] <= note[i + 1]:
                part[i] = part[i - 1] + 1
            else:
                part[i] = 5
        if note[i] < note[i - 1]:
            if note[i] >= note[i + 1]:
                part[i] = part[i - 1] - 1
            else:
                part[i] = 1
        if note[i] == note[i - 1]:
            if note[i] < note[i + 1]:
                if part[i - 1] != 1:
                    part[i] = 1
                else:
                    part[i] = 2
            elif note[i] > note[i + 1]:
                if part[i - 1] == 5:
                    part[i] = 4
                else:
                    part[i] = 5
            elif part[i - 1] == 2:
                part[i] = 3
            else:
                part[i] = 2
        if not (part[i] <= 5 and part[i] >= 1 and (note[i] > note[i - 1] and part[i] > part[i - 1] or (note[i] < note[i - 1] and part[i] < part[i - 1]) or (note[i] == note[i - 1] and part[i] != part[i - 1]))):
            done = False
            break
    if note[-1] > note[-2]:
        part[-1] = 5
    elif note[-1] < note[-2]:
        part[-1] = 1
    elif part[-2] == 2:
        part[-1] = 3
    else:
        part[-1] = 2
    if not (part[-1] <= 5 and part[-1] >= 1 and (note[-1] > note[-2] and part[-1] > part[-2] or (note[-1] < note[-2] and part[-1] < part[-2]) or (note[-1] == note[-2] and part[-1] != part[-2]))):
        done = False
    if done:
        part = [str(i) for i in part]
        print(' '.join(part))
    else:
        print(-1)
n = int(input())
melody = list(map(int, input().split()))
n = len(melody)
good = True
out = []
i = 0
finger = -1
if n > 1:
    if melody[0] < melody[1]:
        finger = 1
    elif melody[0] > melody[1]:
        finger = 5
    else:
        finger = 3
else:
    finger = 2
i = 1
out.append(finger)
while i < n:
    if i < n - 1:
        if melody[i] < melody[i + 1]:
            if melody[i - 1] < melody[i]:
                if finger == 5:
                    good = False
                else:
                    finger += 1
            elif melody[i - 1] > melody[i]:
                if finger == 1:
                    good = False
                else:
                    finger = 1
            elif finger == 1:
                finger = 2
            else:
                finger = 1
        elif melody[i] > melody[i + 1]:
            if melody[i - 1] < melody[i]:
                if finger == 5:
                    good = False
                else:
                    finger = 5
            elif melody[i - 1] > melody[i]:
                if finger == 1:
                    good = False
                else:
                    finger -= 1
            elif finger == 5:
                finger = 4
            else:
                finger = 5
        elif melody[i - 1] < melody[i]:
            if finger == 5:
                good = False
            else:
                finger += 1
        elif melody[i - 1] > melody[i]:
            if finger == 1:
                good = False
            else:
                finger -= 1
        elif finger == 3:
            finger = 2
        else:
            finger = 3
    elif melody[i - 1] < melody[i]:
        if finger == 5:
            good = False
        else:
            finger += 1
    elif melody[i - 1] > melody[i]:
        if finger == 1:
            good = False
        else:
            finger -= 1
    elif finger == 3:
        finger = 2
    else:
        finger = 3
    if not good:
        break
    else:
        out.append(finger)
        i += 1
if good:
    for x in out:
        print(x, end=' ')
else:
    print(-1)