-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equal.java
62 lines (61 loc) · 1.3 KB
/
Equal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Equal
{
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
String s;
StringTokenizer st;
int T=Integer.parseInt(br.readLine().trim());
while(T-->0)
{
int N=Integer.parseInt(br.readLine().trim());
int arr[]=new int[N];
s=br.readLine().trim();
st=new StringTokenizer(s);
int min=Integer.MAX_VALUE;
for(int i=0;i<N;i++)
{
arr[i]=Integer.parseInt(st.nextToken());
min=Math.min(min, arr[i]);
}
int minop=Integer.MAX_VALUE;
int size=5;
if(min<size)
size=min;
int total[]=new int[size+1];
for(int i=0;i<=size;i++)
{
for(int j=0;j<N;j++)
{
int left=arr[j]-(min-i);
total[i]+=reduce(left);
}
minop=Math.min(minop,total[i]);
}
System.out.println(minop);
}
}
private static int reduce(int left)
{
int steps=0;
if(left>=5)
{
steps=left/5;
left=left%5;
}
if(left>=2)
{
steps+=left/2;
left=left%2;
}
steps+=left;
return steps;
}
}