結果

問題 No.1765 While Shining
ユーザー ks2m
提出日時 2021-11-26 22:36:45
言語 Java
(openjdk 23)
結果
AC  
実行時間 727 ms / 2,000 ms
コード長 798 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 77,540 KB
実行使用メモリ 53,540 KB
最終ジャッジ日時 2024-06-29 18:15:40
合計ジャッジ時間 15,290 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		sc.close();

		if (n == 1) {
			System.out.println(0);
			return;
		}

		long[] num = new long[n + 1];
		num[1] = 1;
		num[2] = 2;
		for (int i = 3; i < num.length; i++) {
			num[i] = num[i - 2] + i;
		}

		long ans = 0;
		int cnt = 0;
		for (int i = 0; i < n - 1; i++) {
			if (cnt % 2 == 0) {
				if (a[i] == 1) {
					cnt++;
				} else {
					ans += num[cnt];
					cnt = 0;
				}
			} else {
				if (a[i] == 1) {
					ans += num[cnt];
					cnt = 1;
				} else {
					cnt++;
				}
			}
		}
		ans += num[cnt];
		System.out.println(ans);
	}
}
0