結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー tentententen
提出日時 2020-11-18 16:19:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 788 ms / 2,000 ms
コード長 1,122 bytes
コンパイル時間 1,990 ms
コンパイル使用メモリ 73,484 KB
実行使用メモリ 66,784 KB
最終ジャッジ日時 2023-09-30 15:05:13
合計ジャッジ時間 17,213 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,716 KB
testcase_01 AC 125 ms
55,412 KB
testcase_02 AC 124 ms
55,932 KB
testcase_03 AC 140 ms
55,608 KB
testcase_04 AC 135 ms
55,684 KB
testcase_05 AC 136 ms
55,644 KB
testcase_06 AC 135 ms
55,632 KB
testcase_07 AC 135 ms
55,860 KB
testcase_08 AC 135 ms
55,972 KB
testcase_09 AC 136 ms
55,512 KB
testcase_10 AC 136 ms
55,768 KB
testcase_11 AC 137 ms
55,696 KB
testcase_12 AC 137 ms
55,756 KB
testcase_13 AC 270 ms
60,316 KB
testcase_14 AC 254 ms
60,076 KB
testcase_15 AC 252 ms
60,188 KB
testcase_16 AC 291 ms
60,044 KB
testcase_17 AC 257 ms
59,388 KB
testcase_18 AC 281 ms
59,748 KB
testcase_19 AC 275 ms
60,032 KB
testcase_20 AC 259 ms
59,928 KB
testcase_21 AC 271 ms
60,640 KB
testcase_22 AC 253 ms
59,612 KB
testcase_23 AC 783 ms
66,588 KB
testcase_24 AC 761 ms
66,784 KB
testcase_25 AC 755 ms
66,636 KB
testcase_26 AC 788 ms
66,368 KB
testcase_27 AC 753 ms
66,516 KB
testcase_28 AC 757 ms
66,412 KB
testcase_29 AC 769 ms
66,784 KB
testcase_30 AC 762 ms
66,696 KB
testcase_31 AC 775 ms
66,740 KB
testcase_32 AC 729 ms
66,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] lefts = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            if (i == 0) {
                lefts[i] = arr[i];
            } else {
                lefts[i] = Math.min(arr[i], lefts[i - 1]);
            }
        }
        int[] rights = new int[n];
        rights[n - 1] = arr[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            rights[i] = Math.min(arr[i], rights[i + 1]);
        }
        int min = Integer.MAX_VALUE;
        for (int i = 1; i < n - 1; i++) {
            int front = lefts[i - 1];
            int rear = rights[i + 1];
            if ((arr[i] > front && arr[i] > rear) || (arr[i] < front && arr[i] < rear)) {
                min = Math.min(min, arr[i] + rear + front);
            }
        }
        if (min == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
    }
}
0