def parse(inFile):
words = inFile.getWords()[1:]
return [(words[2*i],int(words[2*i+1])) for i in xrange(len(words)/2)]
def solve(data):
pos = {'O':1,'B':1}
time = {'O':0,'B':0}
other = {'O':'B','B':'O'}
for datum in data:
[robot, button] = datum
time[robot] = time[robot] + 1 + abs(button - pos[robot])
pos[robot] = button
if (time[robot] <= time[other[robot]]):
time[robot] = time[other[robot]] + 1
return max(time.values())
if __name__ == "__main__":
from GCJ import GCJ
GCJ(parse, solve, "/Users/lpebody/gcj/2011_q/", "a").run()
ZGVmIHBhcnNlKGluRmlsZSk6CiAgICB3b3JkcyA9IGluRmlsZS5nZXRXb3JkcygpWzE6XQogICAgcmV0dXJuIFsod29yZHNbMippXSxpbnQod29yZHNbMippKzFdKSkgZm9yIGkgaW4geHJhbmdlKGxlbih3b3JkcykvMildCgpkZWYgc29sdmUoZGF0YSk6CiAgICBwb3MgPSB7J08nOjEsJ0InOjF9CiAgICB0aW1lID0geydPJzowLCdCJzowfQogICAgb3RoZXIgPSB7J08nOidCJywnQic6J08nfQogICAgZm9yIGRhdHVtIGluIGRhdGE6CiAgICAgICAgW3JvYm90LCBidXR0b25dID0gZGF0dW0KICAgICAgICB0aW1lW3JvYm90XSA9IHRpbWVbcm9ib3RdICsgMSArIGFicyhidXR0b24gLSBwb3Nbcm9ib3RdKQogICAgICAgIHBvc1tyb2JvdF0gPSBidXR0b24KICAgICAgICBpZiAodGltZVtyb2JvdF0gPD0gdGltZVtvdGhlcltyb2JvdF1dKToKICAgICAgICAgICAgdGltZVtyb2JvdF0gPSB0aW1lW290aGVyW3JvYm90XV0gKyAxCiAgICByZXR1cm4gbWF4KHRpbWUudmFsdWVzKCkpCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgZnJvbSBHQ0ogaW1wb3J0IEdDSgogICAgR0NKKHBhcnNlLCBzb2x2ZSwgIi9Vc2Vycy9scGVib2R5L2djai8yMDExX3EvIiwgImEiKS5ydW4oKQoKICAgICAgICAgICAgCg==