Here is the solution link.
https://codeforces.me/contest/1798/submission/201240362
But suddenly Codeforces was giving "wrong output format Unexpected end of file — int32 expected (test case 4481)". I'm not familiar with this. Can anyone plz explain it ??