Tutorial is loading...
Code (C++ version)
Tutorial is loading...
Code (C++ version)
Code (Python version)
Tutorial is loading...
Code (C++ version)
Code (Python version)
Tutorial is loading...
Code (C++ version)
Tutorial is loading...
Code (C++ version)
Tutorial is loading...
Code (My stupid solution: C++ version)
Code (demon1999's fast solution: C++11 version)
UPD1: Note that the editorial of problem E is modified with some correction. Sorry for that inconvenience.
UPD2: The editorial is complete now. Hope you find it useful. :P
UPD3: It's seems that all bonus questions can be found in the comments. :) Don't hesitate to give them an upvote. :P
UPD4: The editorial is updated.