Here is the solution link.
https://mirror.codeforces.com/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 ??