Problema adaptado de \href{https://codeforces.com/contest/1405/problem/D}{Codeforces Round 668 (Div. 2, Problem D)}.