結果

問題 No.297 カードの数式
ユーザー GrenacheGrenache
提出日時 2015-11-06 22:52:13
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,709 bytes
コンパイル時間 5,457 ms
コンパイル使用メモリ 79,708 KB
実行使用メモリ 56,168 KB
最終ジャッジ日時 2023-10-11 14:46:29
合計ジャッジ時間 7,988 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,884 KB
testcase_01 AC 128 ms
55,844 KB
testcase_02 AC 128 ms
55,724 KB
testcase_03 AC 129 ms
55,448 KB
testcase_04 WA -
testcase_05 AC 129 ms
56,036 KB
testcase_06 WA -
testcase_07 AC 127 ms
55,784 KB
testcase_08 AC 129 ms
55,956 KB
testcase_09 AC 128 ms
55,632 KB
testcase_10 AC 128 ms
55,756 KB
testcase_11 AC 128 ms
55,656 KB
testcase_12 AC 130 ms
55,824 KB
testcase_13 AC 129 ms
55,612 KB
testcase_14 AC 128 ms
55,924 KB
testcase_15 AC 128 ms
55,904 KB
testcase_16 AC 127 ms
56,060 KB
testcase_17 AC 130 ms
55,760 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 128 ms
55,648 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Main_yukicoder297 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        char[] c = new char[n];
        int p = 0;
        int m = 0;
        int[] d = new int[10];
        for (int i = 0; i < n; i++) {
        	c[i] = sc.next().charAt(0);
        	if (c[i] == '+') {
        		p++;
        	} else if (c[i] == '-') {
        		m++;
        	} else {
        		d[c[i] - '0']++;
        	}
        }
        
        int[] num = new int[p + m + 1];
        for (int i = 0; i < p + m + 1 - 1; i++) {
        	for (int j = 0; j < 10; j++) {
        		if (d[j] > 0) {
        			num[i] = j;
        			d[j]--;
        			break;
        		}
        	}
        }

        int tmp = 0;
        for (int i = 9; i >= 0; i--) {
        	while (d[i] > 0) {
        		tmp = tmp * 10 + i;
        		d[i]--;
        	}
        }
        num[p + m + 1 - 1] = tmp;

        int max = 0;
        int tmpp = p;
        int tmpm = m;
        for (int i = p + m; i >= 0; i--) {
        	if (i == p + m) {
        		max += num[i];
        	} else if (tmpp > 0) {
        		max += num[i];
        		tmpp--;
        	} else if (tmpm > 0) {
        		max -= num[i];
        		tmpm--;
        	}
        }

        int min = 0;
        tmpp = p;
        tmpm = m;
        for (int i = 0; i < p + m + 1; i++) {
        	if (i == 0) {
        		min += num[i];
        	} else if (tmpp > 0) {
        		min += num[i];
        		tmpp--;
        	} else if (tmpm > 0) {
        		min -= num[i];
        		tmpm--;
        	}
        }
        
        System.out.printf("%d %d\n", max, min);

        sc.close();
    }
}
0