Problem:
Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.

Solution in Python (3.4.3) (judge verdict: Runtime Error)

def sub(A):
    if len(A) == 1:
        return (A[0], None), (A[0], None)
    min_sub_list = []
    max_sub_list = []
    for i in range(len(A)):
        A_i = A.copy()
        a_i = A_i.pop(i)
        res_min_i, res_max_i = sub(A_i)
        b_i_min, pair_i_min = res_min_i
        b_i_max, pair_i_max = res_max_i
        min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
        max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
    res_min = min(min_sub_list)
    res_max = max(max_sub_list)
    return res_min, res_max


def pair_extract(p, pair_list):
    if p is not None:
        a = p[0]
        b = p[1]
        p_next = p[2]
        pair_list.append((a, b))
        pair_extract(p_next, pair_list)


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
_, res_max = sub(A)
max_val = res_max[0]
pair_list = []
pair_extract(res_max[1], pair_list)
pair_list.reverse()
print(max_val)
for p in pair_list:
    print(p[0], p[1])