solved 3669, but timelimitexceeded
solved 1017