The first question is from this http://acm.ashland.edu/2012/Problem-Set/Problems/2012.pdf the The 2012 ACM-ICPC East Central North America problem A