https://tproger.ru/problems/logic-problem-for-weights