codeforces; solve "Tram" from Beta Round #87 (Div. 2)