結果

問題 No.2335 Jump
ユーザー ks2m
提出日時 2023-06-02 21:41:46
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 847 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 78,472 KB
実行使用メモリ 62,816 KB
最終ジャッジ日時 2024-12-28 16:59:33
合計ジャッジ時間 6,755 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 2 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		sa = br.readLine().split(" ");
		int[] a = new int[n];
		Set<Integer> set = new HashSet<>();
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
			if (a[i] <= n) {
				set.add(a[i]);
			}
		}
		br.close();

		long ans = 0;
		int goal = n;
		for (int i = n - 1; i >= 0; i--) {
			while (set.contains(goal)) {
				set.remove(goal);
				goal--;
			}
			if (goal == 0) {
				break;
			}
			ans += a[i] - goal - (n - set.size() - 1);
			set.add(goal);
		}
		System.out.println(ans);
	}
}
0