solved 189: a bit ugly, DP = 5 sec