結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー ks2mks2m
提出日時 2020-06-27 00:17:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 897 ms / 2,000 ms
コード長 1,487 bytes
コンパイル時間 2,313 ms
コンパイル使用メモリ 75,676 KB
実行使用メモリ 88,528 KB
最終ジャッジ日時 2023-09-18 09:58:16
合計ジャッジ時間 13,977 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,532 KB
testcase_01 AC 42 ms
49,424 KB
testcase_02 AC 42 ms
49,284 KB
testcase_03 AC 45 ms
49,396 KB
testcase_04 AC 44 ms
49,672 KB
testcase_05 AC 44 ms
49,540 KB
testcase_06 AC 45 ms
49,480 KB
testcase_07 AC 45 ms
49,544 KB
testcase_08 AC 46 ms
49,480 KB
testcase_09 AC 45 ms
47,472 KB
testcase_10 AC 44 ms
49,424 KB
testcase_11 AC 45 ms
49,496 KB
testcase_12 AC 44 ms
49,440 KB
testcase_13 AC 143 ms
55,540 KB
testcase_14 AC 148 ms
55,444 KB
testcase_15 AC 145 ms
55,716 KB
testcase_16 AC 141 ms
55,560 KB
testcase_17 AC 138 ms
55,816 KB
testcase_18 AC 143 ms
55,424 KB
testcase_19 AC 140 ms
55,148 KB
testcase_20 AC 145 ms
55,712 KB
testcase_21 AC 141 ms
55,556 KB
testcase_22 AC 132 ms
55,444 KB
testcase_23 AC 847 ms
85,100 KB
testcase_24 AC 839 ms
84,876 KB
testcase_25 AC 816 ms
84,132 KB
testcase_26 AC 825 ms
84,188 KB
testcase_27 AC 867 ms
85,532 KB
testcase_28 AC 642 ms
86,680 KB
testcase_29 AC 621 ms
86,540 KB
testcase_30 AC 876 ms
88,268 KB
testcase_31 AC 857 ms
88,528 KB
testcase_32 AC 897 ms
88,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TreeMap;
import java.util.TreeSet;

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

		TreeMap<Integer, Integer> map = new TreeMap<>();
		for (int i = 0; i < n; i++) {
			map.put(a[i], i);
		}

		TreeSet<Integer> set = new TreeSet<>();
		boolean h = false;
		int ans = Integer.MAX_VALUE;
		for (Integer k : map.keySet()) {
			if (set.size() == 0) {
				set.add(map.get(k));
			} else if (set.size() == 1) {
				if (set.last() < map.get(k)) {
					h = true;
				}
				set.add(map.get(k));
			} else {
				if (h && set.last() > map.get(k)) {
					int f = set.pollFirst();
					int v1 = a[f];
					int v2 = a[map.get(k)];
					int v3 = a[set.higher(map.get(k))];
					ans = Math.min(ans, v1 + v2 + v3);
					set.add(f);
				} else if (!h && set.first() < map.get(k)) {
					int f = set.pollLast();
					int v1 = a[f];
					int v2 = a[map.get(k)];
					int v3 = a[set.lower(map.get(k))];
					ans = Math.min(ans, v1 + v2 + v3);
					set.add(f);
				} else {
					set.add(map.get(k));
				}
			}
		}
		if (ans == Integer.MAX_VALUE) {
			System.out.println(-1);
		} else {
			System.out.println(ans);
		}
	}
}
0