結果

問題 No.3130 Twin's Add Max Min Game
ユーザー ks2m
提出日時 2025-04-25 23:19:36
言語 Java
(openjdk 23)
結果
AC  
実行時間 584 ms / 2,000 ms
コード長 1,272 bytes
コンパイル時間 3,301 ms
コンパイル使用メモリ 80,532 KB
実行使用メモリ 86,084 KB
最終ジャッジ日時 2025-04-25 23:20:20
合計ジャッジ時間 30,750 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

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]);
		}
		sa = br.readLine().split(" ");
		br.close();

		int add = 0;
		int max = 0;
		int min = 0;
		for (int i = 0; i < n; i++) {
			if (sa[i].equals("add")) add++;
			if (sa[i].equals("max")) max++;
			if (sa[i].equals("min")) min++;
		}
		Arrays.sort(a);

		if (add == n) {
			long ans = 0;
			for (int i = 0; i < n; i++) {
				ans += a[i];
			}
			System.out.println(ans);
			return;
		}
		if (max == n) {
			System.out.println(a[n - 1]);
			return;
		}
		if (min == n) {
			System.out.println(0);
			return;
		}

		if (min == 0) {
			long ans = a[n - 1];
			for (int i = 0; i < add - 1; i++) {
				ans += a[i];
			}
			System.out.println(ans);
		} else {
			int ans1 = a[n - min];
			long ans2 = a[n - min - 1];
			for (int i = 0; i < add - 1; i++) {
				ans2 += a[i];
			}
			long ans = Math.min(ans1, ans2);
			System.out.println(ans);
		}
	}
}
0