using System;
using System.Collections;
using System.Collections.Generic;
public class Program {
public static void Main() {
string line;
while ((line = System.Console.ReadLine ()) != null) { // 注意 while 处理多个 case
int n = int.Parse(line);
int[] a = new int[n];
line = Console.ReadLine();
string[] tokens = line.Split(' ');
for(int i = 0; i < n; i++) a[i] = int.Parse(tokens[i]);
int sum = 0;
foreach(int i in a) sum += i;
if(((float)sum / n) % 1 != 0) Console.WriteLine(-1);
else{
int avg = sum / n;
int remain = avg % 2;
int count = 0;
foreach(int ai in a){
if(ai % 2 != remain){
count = -1;
break;
}
else{
if(ai > avg){
count += (ai - avg) / 2;
}
}
}
Console.WriteLine(count);
}
}
}
}
复杂度O(N)。先计算苹果的平均值,平均值不为整数直接输出-1;对每只牛的苹果遍历,若某只牛的苹果和平均值的奇偶不同,令
count=-1;break;若奇偶相同,且苹果数大于平均值,计算每多2个,count++

京公网安备 11010502036488号