简单的水题

网易牛分苹果

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] cow_apples = new int[n];
        int i = 0, sum = 0, tmp = 0;
        while (i < n){
            tmp = scanner.nextInt();
            cow_apples[i++] = tmp;
            sum += tmp;
        }
        if (sum % n != 0){
            System.out.println(-1);
            return;
        }
        int target = sum / n;
        Arrays.sort(cow_apples);
        int count = 0;
        for (int j:
                cow_apples){
            if (((j - target) & 1) == 1){
                System.out.println(-1);
                return;
            }
            if (j < target){
                count += ((target -  j) >> 1);
            }
        }
        System.out.println(count);
    }
}

你可能感兴趣的:(简单的水题)