#GD0000173. Cân bằng số dư (Balanced Remainders)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cân bằng số dư (Balanced Remainders)
Balanced Remainders
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of nonnegative integers, where is divisible by . In one move, choose any element and increase it by exactly . The same element may be increased repeatedly.
Let be the numbers of elements whose remainders modulo are , respectively. The array is balanced when .
Find the minimum number of moves required to balance the remainder counts.
Input
Line 1 contains the integer , the length of the array.
Line 2 contains exactly integers in their initial order.
Output
Print one nonnegative integer: the minimum number of unit increments needed to equalize the three remainder counts.
Subtasks
- Subtask 1 (20 points): ; ; for every .
- Subtask 2 (30 points): ; ; for every .
- Subtask 3 (50 points): ; ; for every .
Examples
Input
6
0 2 5 5 4 8
Output
3
Explanation
The initial remainders are , giving . Increasing the first to changes the counts to . Increasing the last to gives . Increasing to gives . These are three unit moves, and fewer cannot transfer enough items through the required remainder classes.