How do i improve my approach towards a problem ?

The problem is simple, the solution is simple but I don’t know why I always end up with a complex approach and hence get a WA. It had happened many times.
Yesterday in codeforces educational round 53. For problem one I come up with this one
but when I saw the solutions of other contestants after the contest, they had very simple and easy to understand code.