# Arup Guha
# 3/1/2025
# Brute Force Code

def odom(arr, k, numD):

    # We filled it in!
    if k == len(arr):
        printf(arr)
        return

    # Try each possible digit, 0,1,...,numD-1 in slot k.
    for i in range(numD):
        arr[k] = i
        odom(arr, k+1, numD)

# Solves the subset sum problem on the array values determining if there's
# a subset that adds up exactly to target or not. arr represents the current
# "odometer" and k represents what's filled in.
def subsetsum(values, target, arr, k):

    # Filled in a subset evaluate it.
    if k == len(arr):

        # Add up values in this subset.
        mytot = 0
        for i in range(len(arr)):
            mytot += arr[i]*values[i]

        # We return true only if the sum is exact.
        return mytot == target

    # There are two options, not in the subset(0), or in it(1).
    for i in range(2):
        
        arr[k] = i
        
        # See if this works, if it does, immediately return True.
        tmpRes = subsetsum(values, target, arr, k+1)
        if tmpRes:
            return True
        
    # Neither including or not including item k worked, so we can't do it.
    return False

# Prints all permutations of 0..n-1 (n = len(perm)), where the first k items in perm are fixed.
def printperm(perm, used, k):

    # Process a completed permutation.
    if k == len(perm):
        printf(perm)
        return

    # Try each item in slot k.
    for i in range(len(perm)):

        # Skip over used items.
        if used[i]:
            continue

        # Place i in slot k, reserve it.
        perm[k] = i
        used[i] = True

        # Recursively print all permutations with these k+1 slots filled.
        printperm(perm, used, k+1)

        # Unreserve this item so it can be used later.
        used[i] = False

# Returns [val, myperm] where val is the maximum climb and myperm is the first lexicographical
# permutation that achieves that answer, when the first k items of perm are fixed.
def bestclimb(perm, used, k):

    # Process a completed permutation.
    if k == len(perm):

        # Store amount of climb here.
        res = 0

        # For each segment, add the amount we go up or down.
        for i in range(len(perm)-1):
            res += abs(perm[i] - perm[i+1])

        # Return my result.
        mycopy = []
        for x in perm:
            mycopy.append(x)
        return [res, mycopy]

    # Our answer will be at least this good.
    num = 0
    bestlist = []

    # Try each item in slot k.
    for i in range(len(perm)):

        # Skip over used items.
        if used[i]:
            continue

        # Place i in slot k, reserve it.
        perm[k] = i
        used[i] = True

        # Recursively get best climb with these k+1 slots filled in.
        # Update our answer if necessary.
        tmp = bestclimb(perm, used, k+1)
        if tmp[0] > num:
            num = tmp[0]
            bestlist = tmp[1]

        # Unreserve this item so it can be used later.
        used[i] = False

    # Ta da!
    return [num, bestlist]

# Prints arr without spaces or commas.
def printf(arr):
    for i in range(len(arr)):
        print(arr[i], end="")
    print()

# Test the odometer.
def testodom():
    myodometer = [0]*4
    odom(myodometer, 0, 3)

# Test subset sum code.
def testsubsetsum():
    vals = [27, 18, 99, 13, 16]
    for target in range(1, 200):
        res = subsetsum(vals, target, [0]*len(vals), 0)
        if res:
            print(target, end=", ")
    print()

# Test the permutation code.
def testperm():
    myperm = [0]*5
    used = [False]*5
    printperm(myperm, used, 0)

# Test the climb code.
def testclimb():
    n = int(input("What is your permutation size?\n"))
    res = bestclimb([0]*n, [False]*n, 0)
    print("climb size is", res[0])
    print("perm is ", res[1])

# Run whatever you want here.
testclimb()
                

