https://pastebin.com/9YLB3iWu - Примерно решение
Задачата я има 4 пъти в Judje:
https://judge.softuni.bg/Contests/Practice/Index/91#0 - Авторско решение + тестовете от Judje можеш да видиш къде ти гърми.
https://judge.softuni.bg/Contests/Practice/Index/455#1
https://judge.softuni.bg/Contests/Practice/Index/331#1
https://judge.softuni.bg/Contests/Compete/Index/447#1