[Update] Solution worked for sample input but timed out for actual input. Fix postponed :(