PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000173. Cân bằng số dư (Balanced Remainders)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cân bằng số dư (Balanced Remainders)

Balanced Remainders

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array aa of nn nonnegative integers, where nn is divisible by 33. In one move, choose any element and increase it by exactly 11. The same element may be increased repeatedly.

Let c0,c1,c2c_0,c_1,c_2 be the numbers of elements whose remainders modulo 33 are 0,1,20,1,2, respectively. The array is balanced when c0=c1=c2=n/3c_0=c_1=c_2=n/3.

Find the minimum number of moves required to balance the remainder counts.

Input

Line 1 contains the integer nn, the length of the array.

Line 2 contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n 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): 3≤n≤303\le n\le30; 3∣n3\mid n; 0≤ai≤1000\le a_i\le100 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 3≤n≤30003\le n\le3000; 3∣n3\mid n; 0≤ai≤1000\le a_i\le100 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 3≤n≤3⋅1043\le n\le3\cdot10^4; 3∣n3\mid n; 0≤ai≤1000\le a_i\le100 for every 1≤i≤n1\le i\le n.

Examples

Input

6
0 2 5 5 4 8

Output

3

Explanation

The initial remainders are 0,2,2,2,1,20,2,2,2,1,2, giving (c0,c1,c2)=(1,1,4)(c_0,c_1,c_2)=(1,1,4). Increasing the first 55 to 66 changes the counts to (2,1,3)(2,1,3). Increasing the last 88 to 99 gives (3,1,2)(3,1,2). Increasing 00 to 11 gives (2,2,2)(2,2,2). These are three unit moves, and fewer cannot transfer enough items through the required remainder classes.