結果

問題 No.198 キャンディー・ボックス2
ユーザー kenji_shioya
提出日時 2016-06-16 06:14:30
言語 Java
(openjdk 23)
結果
AC  
実行時間 138 ms / 1,000 ms
コード長 699 bytes
コンパイル時間 4,974 ms
コンパイル使用メモリ 75,024 KB
実行使用メモリ 46,080 KB
最終ジャッジ日時 2024-11-16 10:03:23
合計ジャッジ時間 9,685 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Exercise88{
  public static void main (String[] args){

    Scanner sc = new Scanner(System.in);

    long h = sc.nextInt();
    int n = sc.nextInt();
    long[] box = new long[n];
    long sum = 0;
    long ave = 0;

    for(int i = 0; i < n; i++){
      box[i] = sc.nextInt();
      sum += box[i];
    }
    sum += h;
    Arrays.sort(box);
    ave = sum / (long)n;
    ave = Math.min(sum / n, box[n / 2]);
    long answer = 0;
    for(int i = 0; i < box.length; i++){
      long x = box[i] - ave;
      if(x == 0){
        continue;
      }else if(x < 0){
      answer += x * -1;
      }else{
      answer += x;
      }
    }
    System.out.println(answer);
  }
}
0