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 Rust (1.15.1) (judge verdict: Runtime Error)

macro_rules! get {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    get!($t)
        ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    get!($($t),*)
        ).collect::<Vec<_>>()
    };
    ($t:ty ;;) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
    ($t:ty ;; $n:expr) => {
        (0..$n).map(|_| get!($t ;;)).collect::<Vec<_>>()
    };
}

fn count(mat: Vec<Vec<(i64, i64)>>, n: (i64, i64)) -> u64 {
    let mut cnt = 0;
    for (x, row) in mat.into_iter().enumerate() {
        for (y, m) in row.into_iter().enumerate() {
            if n == m {
                cnt+=1;
            }
        }
    }
    return cnt;
}

fn most_common(mat: Vec<Vec<(i64, i64)>>) -> (i64, i64) {
    let mut most_common = (0, 0);
    let mut most_common_cnt = 0;
    let mat2 = mat.clone();
    for (x, row) in mat.into_iter().enumerate() {
        for (y, n) in row.into_iter().enumerate() {
            if y == x {
                continue;
            }
            let cnt = count(mat2.clone(), n);
            if cnt > most_common_cnt {
                most_common_cnt = cnt;
                most_common = n;
            }
        }
    }
    return most_common;
}

fn rec(prev: (i64, i64), xys: Vec<(i64, i64)>, p: i64, q: i64) -> u64 {
    let mut min_cost = 100000;
    for (i, xy) in xys.clone().iter().enumerate() {
        let mut cost = if xys.len() > 1 {
            rec(*xy, xys.iter().enumerate().filter(|&(k, _)| k != i).map(|(_, e)| *e).collect(), p, q)
        } else {
            0
        };
        cost += if xy.0 - prev.0 == p && xy.1 - prev.1 == q {
            0
        } else {
            1
        };
        if cost < min_cost {
            min_cost = cost;
        }
    }
    return min_cost;
}


fn main() {
    let N = get!(u8);
    let xys: Vec<(i64, i64)> = get!(i64, i64; N);

    let mut pqmat: Vec<Vec<(i64, i64)>> = Vec::new();
    let mut cnt = 0;
    for (x1, y1) in xys.clone() {
        pqmat.push(Vec::new());
        for (x2, y2) in xys.clone() {
            pqmat[cnt].push((x1 - x2, y1 - y2));
        }
        cnt+=1;
    }
    let (p, q) = most_common(pqmat);
    let cost = rec((-9839425876, -35437953752), xys, -p, -q);
    println!("{}", cost);
}