Why Runtime Error?
https://atcoder.jp/contests/abc161/submissions/35445063
I think it is obviously a greedy algorithm problem.