結果
| 問題 |
No.1368 サイクルの中に眠る門松列
|
| コンテスト | |
| ユーザー |
ks2m
|
| 提出日時 | 2021-01-29 22:01:15 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 422 ms / 2,000 ms |
| コード長 | 1,461 bytes |
| コンパイル時間 | 2,117 ms |
| コンパイル使用メモリ | 77,688 KB |
| 実行使用メモリ | 70,552 KB |
| 最終ジャッジ日時 | 2024-06-27 08:14:26 |
| 合計ジャッジ時間 | 8,067 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 15 |
ソースコード
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;
}
}
ks2m