Codeforces Round #231 (Div. 2), Problem D performance optimization