def parse(inFile):
return [inFile.getInts() for i in xrange(inFile.getInt())]
def solve(levels):
INFINITY = 2 * len(levels) + 1
starrating = 0
levelsDone = 0
justCantDoIt = False
while (len(levels) > 0):
score = sum([2 if level[0] < INFINITY else 1 for level in levels if level[1] <= starrating])
levelsDone += len([level for level in levels if level[1] <= starrating])
levels = [level for level in levels if level[1] > starrating]
starrating += score
if (len([level for level in levels if level[1] <= starrating])):
continue
if len(levels) == 0:
break
if len([level for level in levels if level[0] <= starrating]) == 0:
justCantDoIt = True
break
holder = -1
for levelIndex in xrange(len(levels)):
if levels[levelIndex][0] <= starrating:
if (holder == -1) or (levels[levelIndex][1] > levels[holder][1]):
holder = levelIndex
levels[holder][0] = INFINITY
starrating += 1
levelsDone += 1
if justCantDoIt:
return "Too Bad"
else:
return levelsDone
if __name__ == "__main__":
from GCJ import GCJ
GCJ(parse, solve, "/Users/lpebody/gcj/2012_1a/", "b").run()
ZGVmIHBhcnNlKGluRmlsZSk6CiAgICByZXR1cm4gW2luRmlsZS5nZXRJbnRzKCkgZm9yIGkgaW4geHJhbmdlKGluRmlsZS5nZXRJbnQoKSldCgpkZWYgc29sdmUobGV2ZWxzKToKICAgIElORklOSVRZID0gMiAqIGxlbihsZXZlbHMpICsgMQogICAgc3RhcnJhdGluZyA9IDAKICAgIGxldmVsc0RvbmUgPSAwCiAgICBqdXN0Q2FudERvSXQgPSBGYWxzZQogICAgd2hpbGUgKGxlbihsZXZlbHMpID4gMCk6CiAgICAgICAgc2NvcmUgPSBzdW0oWzIgaWYgbGV2ZWxbMF0gPCBJTkZJTklUWSBlbHNlIDEgZm9yIGxldmVsIGluIGxldmVscyBpZiBsZXZlbFsxXSA8PSBzdGFycmF0aW5nXSkKICAgICAgICBsZXZlbHNEb25lICs9IGxlbihbbGV2ZWwgZm9yIGxldmVsIGluIGxldmVscyBpZiBsZXZlbFsxXSA8PSBzdGFycmF0aW5nXSkKICAgICAgICBsZXZlbHMgPSBbbGV2ZWwgZm9yIGxldmVsIGluIGxldmVscyBpZiBsZXZlbFsxXSA+IHN0YXJyYXRpbmddCiAgICAgICAgc3RhcnJhdGluZyArPSBzY29yZQogICAgICAgIGlmIChsZW4oW2xldmVsIGZvciBsZXZlbCBpbiBsZXZlbHMgaWYgbGV2ZWxbMV0gPD0gc3RhcnJhdGluZ10pKToKICAgICAgICAgICAgY29udGludWUKICAgICAgICBpZiBsZW4obGV2ZWxzKSA9PSAwOgogICAgICAgICAgICBicmVhawogICAgICAgIGlmIGxlbihbbGV2ZWwgZm9yIGxldmVsIGluIGxldmVscyBpZiBsZXZlbFswXSA8PSBzdGFycmF0aW5nXSkgPT0gMDoKICAgICAgICAgICAganVzdENhbnREb0l0ID0gVHJ1ZQogICAgICAgICAgICBicmVhawogICAgICAgIGhvbGRlciA9IC0xCiAgICAgICAgZm9yIGxldmVsSW5kZXggaW4geHJhbmdlKGxlbihsZXZlbHMpKToKICAgICAgICAgICAgaWYgbGV2ZWxzW2xldmVsSW5kZXhdWzBdIDw9IHN0YXJyYXRpbmc6CiAgICAgICAgICAgICAgICBpZiAoaG9sZGVyID09IC0xKSBvciAobGV2ZWxzW2xldmVsSW5kZXhdWzFdID4gbGV2ZWxzW2hvbGRlcl1bMV0pOgogICAgICAgICAgICAgICAgICAgIGhvbGRlciA9IGxldmVsSW5kZXgKICAgICAgICBsZXZlbHNbaG9sZGVyXVswXSA9IElORklOSVRZCiAgICAgICAgc3RhcnJhdGluZyArPSAxCiAgICAgICAgbGV2ZWxzRG9uZSArPSAxCiAgICBpZiBqdXN0Q2FudERvSXQ6CiAgICAgICAgcmV0dXJuICJUb28gQmFkIgogICAgZWxzZToKICAgICAgICByZXR1cm4gbGV2ZWxzRG9uZQoKaWYgX19uYW1lX18gPT0gIl9fbWFpbl9fIjoKICAgIGZyb20gR0NKIGltcG9ydCBHQ0oKICAgIEdDSihwYXJzZSwgc29sdmUsICIvVXNlcnMvbHBlYm9keS9nY2ovMjAxMl8xYS8iLCAiYiIpLnJ1bigpCg==