Upvote:2
Going from 2 weeks to 5 months is a material change - had you requested such a long stay in your application your visit visa may not have been approved. There are other examples of this on TSE eg I was refused a UK visa because of my visit history and Cancelled UK visa on entry
Advice on what to do is simple: If you reapply, you will have to address the refusal reasons given in the decision by providing a convincing explanation and supporting evidence where possible.