class BearPlays():
    def pileSize(self, a, b, k):
        lol = a*pow(2, k, a+b)
        lol %= (a+b)
        return min(lol, a+b-lol)