def parse(inFile):
    i = inFile.getInts()
    return tuple(i[1:3]+[i[3:]])

def solve((S,p,scores)):
    bestunsurprising = 3 * p - 2 if (p > 0) else 0
    bestsurprising = 3 * p - 4 if (p > 2) else p
    return len([score for score in scores if score >= bestunsurprising]) + min(S,len([score for score in scores if bestunsurprising > score and score >= bestsurprising]))

if __name__ == "__main__":
    from GCJ import GCJ
    GCJ(parse, solve, "/Users/lpebody/gcj/2012_q/", "b").run()

            
