結果

問題 No.684 Prefix Parenthesis
ユーザー 37zigen37zigen
提出日時 2018-05-12 23:46:50
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 960 bytes
コンパイル時間 2,406 ms
コンパイル使用メモリ 76,384 KB
実行使用メモリ 63,680 KB
最終ジャッジ日時 2023-09-10 18:34:58
合計ジャッジ時間 10,394 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,996 KB
testcase_01 AC 121 ms
55,580 KB
testcase_02 AC 120 ms
56,432 KB
testcase_03 AC 210 ms
59,012 KB
testcase_04 AC 256 ms
59,692 KB
testcase_05 AC 266 ms
61,948 KB
testcase_06 AC 209 ms
57,216 KB
testcase_07 WA -
testcase_08 AC 153 ms
56,188 KB
testcase_09 WA -
testcase_10 AC 292 ms
62,156 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 247 ms
61,184 KB
testcase_14 AC 254 ms
61,820 KB
testcase_15 AC 215 ms
58,764 KB
testcase_16 AC 260 ms
61,556 KB
testcase_17 AC 228 ms
59,268 KB
testcase_18 AC 129 ms
55,968 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 296 ms
61,620 KB
testcase_22 AC 247 ms
61,700 KB
testcase_23 AC 226 ms
61,324 KB
testcase_24 AC 253 ms
61,860 KB
testcase_25 AC 263 ms
61,488 KB
testcase_26 AC 121 ms
56,012 KB
testcase_27 AC 120 ms
56,160 KB
testcase_28 AC 121 ms
55,732 KB
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	long mo = 1_000_000_000 + 7;

	void run() {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		String str = sc.next();
		long ans = N * (N + 1) / 2;
		long sum = 0;
		long min = 0;
		long[][] a = new long[(int) N][2];
		for (int i = 0; i < N; ++i) {
			sum += (str.charAt(i) == '(' ? 1 : -1);
			ans -= sum;
			min = Math.min(min, sum);
			a[i][0] = sum;
			a[i][1] = min;
		}
		Arrays.sort(a, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return -Long.compare(o1[0], o2[0]);
			}
		});
		sum = 0;
		min = 0;
		for (int i = 0; i < N; ++i) {
			min = Math.min(min, a[i][1] + sum);
			sum += a[i][0];
		}
		System.out.println(ans + 2 * min);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0