結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー ks2mks2m
提出日時 2021-01-29 22:01:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 392 ms / 2,000 ms
コード長 1,461 bytes
コンパイル時間 2,719 ms
コンパイル使用メモリ 74,140 KB
実行使用メモリ 81,064 KB
最終ジャッジ日時 2023-09-09 15:24:42
合計ジャッジ時間 8,335 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,264 KB
testcase_01 AC 45 ms
49,508 KB
testcase_02 AC 237 ms
61,132 KB
testcase_03 AC 141 ms
55,892 KB
testcase_04 AC 163 ms
56,088 KB
testcase_05 AC 392 ms
77,080 KB
testcase_06 AC 359 ms
77,296 KB
testcase_07 AC 346 ms
76,856 KB
testcase_08 AC 371 ms
77,380 KB
testcase_09 AC 374 ms
80,868 KB
testcase_10 AC 341 ms
79,240 KB
testcase_11 AC 344 ms
79,516 KB
testcase_12 AC 343 ms
81,012 KB
testcase_13 AC 338 ms
79,068 KB
testcase_14 AC 340 ms
78,796 KB
testcase_15 AC 346 ms
81,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	static BufferedReader br;
	static PrintWriter pw;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		pw = new PrintWriter(System.out);
		for (int i = 0; i < t; i++) {
			solve();
		}
		br.close();
		pw.flush();
	}

	static void solve() throws Exception {
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n + 2];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}

		long[][] dp = new long[n + 3][3];
		for (int i = 3; i < n + 3; i++) {
			for (int j = 0; j < 3; j++) {
				dp[i][j] = Math.max(dp[i][j], dp[i - 1][j]);
				dp[i][j] = Math.max(dp[i][j], dp[i - 2][j]);
			}

			int[] b = new int[3];
			for (int j = 1; j <= 3; j++) {
				b[j - 1] = a[(i - j) % n];
			}
			if (check(b)) {
				if (i < 6) {
					dp[i][i - 3] = a[i - 3];
				} else {
					for (int j = 0; j < 3; j++) {
						dp[i][j] = Math.max(dp[i][j], dp[i - 3][j] + a[i - 3]);
					}
				}
			}
		}
		long ans = 0;
		for (int i = 0; i < 3; i++) {
			ans = Math.max(ans, dp[n + i][i]);
		}
		pw.println(ans);
	}

	static boolean check(int[] a) {
		if (a[0] != a[2]) {
			if (a[0] < a[1] && a[1] > a[2]) return true;
			if (a[0] > a[1] && a[1] < a[2]) return true;
		}
		return false;
	}
}
0