
import glob
import os

bn = os.path.basename(__file__).split('.')[0]
dn = os.path.dirname(__file__).split('/')

# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        X, K, D = map(int, self.input_data().split())
        if abs(X) > K * D:
            result = abs(X) - K * D
        else:
            if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

        print(str(result))


solver = Solver(REL_PATH)
solver.exec_resolve() import glob glob import os os bn = os.path.basename(__file__).split('.')[0] bn os.path.basename(__file__).split('.')[0] os.path.basename(__file__).split('.') os.path.basename(__file__).split os.path.basename(__file__) os.path.basename os.path os path basename __file__ split '.' 0 dn = os.path.dirname(__file__).split('/') dn os.path.dirname(__file__).split('/') os.path.dirname(__file__).split os.path.dirname(__file__) os.path.dirname os.path os path dirname __file__ split '/' REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn REL_PATH dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' dn[len(dn)-2] + '\\' + dn[len(dn)-1] dn[len(dn)-2] + '\\' dn[len(dn)-2] dn len(dn)-2 len(dn) len dn 2 '\\' dn[len(dn)-1] dn len(dn)-1 len(dn) len dn 1 '\\' bn TOP_PATH = 'C:\\AtCoder' TOP_PATH 'C:\\AtCoder' class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve() Common problem = [] problem [] index = 0 index 0 def __init__(self, rel_path):
        self.rel_path = rel_path

     __init__ self.rel_path = rel_path self.rel_path self rel_path rel_path self self rel_path rel_path def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

     initialize file = open(path) file open(path) open path self.problem = file.readlines() self.problem self problem file.readlines() file.readlines file readlines self.index = 0 self.index self index 0 return self self path path def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

     input_data try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

     except NameError:
            return input()

     return input() input() input IS_TEST self.index += 1 self.index self index 1 return self.problem[self.index-1] self.problem[self.index-1] self.problem self problem self.index-1 self.index self index 1 self self def resolve(self):
        pass

     resolve self self def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve() exec_resolve try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve() except NameError:
            self.resolve() self.resolve() self.resolve self resolve IS_TEST for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
         path glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt') glob.glob glob glob TOP_PATH + '\\' + self.rel_path + '/*.txt' TOP_PATH + '\\' + self.rel_path TOP_PATH + '\\' TOP_PATH '\\' self.rel_path self rel_path '/*.txt' print("Test: " + path) print "Test: " + path "Test: " path self.initialize(path) self.initialize self initialize path self.resolve() self.resolve self resolve print("\n\n") print "\n\n" self self class Solver(Common):

    def resolve(self):

        X, K, D = map(int, self.input_data().split())
        if abs(X) > K * D:
            result = abs(X) - K * D
        else:
            if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

        print(str(result)) Solver def resolve(self):

        X, K, D = map(int, self.input_data().split())
        if abs(X) > K * D:
            result = abs(X) - K * D
        else:
            if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

        print(str(result)) resolve X, K, D = map(int, self.input_data().split()) X map(int, self.input_data().split()) map int self.input_data().split() self.input_data().split self.input_data() self.input_data self input_data split K D if abs(X) > K * D:
            result = abs(X) - K * D
        else:
            if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

         abs(X) > K * D abs(X) abs X K * D K D result = abs(X) - K * D result abs(X) - K * D abs(X) abs X K * D K D if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

         int(abs(X) / D) % 2 == K % 2 int(abs(X) / D) % 2 int(abs(X) / D) int abs(X) / D abs(X) abs X D 2 K % 2 K 2 result = abs(X) - int(abs(X) / D) * D result abs(X) - int(abs(X) / D) * D abs(X) abs X int(abs(X) / D) * D int(abs(X) / D) int abs(X) / D abs(X) abs X D D result = (int(abs(X) / D) + 1) * D - abs(X) result (int(abs(X) / D) + 1) * D - abs(X) (int(abs(X) / D) + 1) * D int(abs(X) / D) + 1 int(abs(X) / D) int abs(X) / D abs(X) abs X D 1 D abs(X) abs X print(str(result)) print str(result) str result self self Common solver = Solver(REL_PATH) solver Solver(REL_PATH) Solver REL_PATH solver.exec_resolve() solver.exec_resolve solver exec_resolve REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn REL_PATH TOP_PATH = 'C:\\AtCoder' 'C:\\AtCoder' TOP_PATH solver = Solver(REL_PATH) Solver(REL_PATH) solver dn = os.path.dirname(__file__).split('/') os.path.dirname(__file__).split('/') dn bn = os.path.basename(__file__).split('.')[0] os.path.basename(__file__).split('.')[0] bn class Solver(Common):

    def resolve(self):

        X, K, D = map(int, self.input_data().split())
        if abs(X) > K * D:
            result = abs(X) - K * D
        else:
            if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

        print(str(result)) class Solver(Common):

    def resolve(self):

        X, K, D = map(int, self.input_data().split())
        if abs(X) > K * D:
            result = abs(X) - K * D
        else:
            if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

        print(str(result)) Solver class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve() class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve() Common

import glob
import os

bn = os.path.basename(__file__).split('.')[0]
dn = os.path.dirname(__file__).split('/')

# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        X, K, D = map(int, self.input_data().split())
        if abs(X) > K * D:
            result = abs(X) - K * D
        else:
            if int(abs(X) / D) % 2 == K % 2:
                result = abs(X) - int(abs(X) / D) * D
            else:
                result = (int(abs(X) / D) + 1) * D - abs(X)

        print(str(result))


solver = Solver(REL_PATH)
solver.exec_resolve()
