Submission #23714328


Source Code Expand

import java.util.*;

public class Main {

	// K個のケーキ
	public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		int k = Integer.parseInt(sc.next());
		int n = Integer.parseInt(sc.next());
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sc.next());
		}
		Arrays.sort(a);

		// コーナーケース ケーキが1種類の場合
		if (n == 1) {
			System.out.println(k - 1);
			return;
		}

		// あとで余るケーキの数
		int amari = Math.max(0, a[n - 1] - a[n - 2] - 1);

		// あとで余るケーキを割り込ませられる隙間の数
		int sukima = 0;
		for (int i = 0; i < n - 2; i++) {
			sukima += a[i];
		}

		// 確認用
//		System.out.println(amari);
//		System.out.println(sukima);

		// こたえ
		if (sukima < amari) {
			System.out.println(amari - sukima);
		} else {
			System.out.println(0);
		}

	}
}

Submission Info

Submission Time
Task B - K Cakes
User tobi00604
Language Java (OpenJDK 11.0.6)
Score 200
Code Size 956 Byte
Status AC
Exec Time 115 ms
Memory 35656 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 200 / 200
Status
AC × 3
AC × 10
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt
Case Name Status Exec Time Memory
0_000.txt AC 115 ms 35636 KiB
0_001.txt AC 103 ms 35396 KiB
0_002.txt AC 105 ms 35436 KiB
1_003.txt AC 109 ms 35548 KiB
1_004.txt AC 105 ms 35440 KiB
1_005.txt AC 110 ms 35656 KiB
1_006.txt AC 103 ms 35400 KiB
1_007.txt AC 105 ms 35560 KiB
1_008.txt AC 107 ms 35624 KiB
1_009.txt AC 110 ms 35568 KiB