結果

問題 No.297 カードの数式
ユーザー GrenacheGrenache
提出日時 2015-11-06 23:02:37
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,714 bytes
コンパイル時間 3,376 ms
コンパイル使用メモリ 74,176 KB
実行使用メモリ 56,332 KB
最終ジャッジ日時 2023-10-11 14:49:21
合計ジャッジ時間 7,953 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,900 KB
testcase_01 AC 125 ms
54,468 KB
testcase_02 AC 126 ms
55,668 KB
testcase_03 AC 124 ms
53,800 KB
testcase_04 AC 126 ms
55,796 KB
testcase_05 AC 127 ms
55,768 KB
testcase_06 WA -
testcase_07 AC 127 ms
55,756 KB
testcase_08 AC 127 ms
55,812 KB
testcase_09 AC 129 ms
55,472 KB
testcase_10 AC 129 ms
55,916 KB
testcase_11 AC 130 ms
55,656 KB
testcase_12 AC 127 ms
55,912 KB
testcase_13 AC 128 ms
55,580 KB
testcase_14 AC 130 ms
55,904 KB
testcase_15 AC 130 ms
56,108 KB
testcase_16 AC 129 ms
55,756 KB
testcase_17 AC 130 ms
55,940 KB
testcase_18 AC 129 ms
55,764 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 128 ms
55,812 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']++;
        	}
        }
        
        long[] num = new long[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;
        		}
        	}
        }

        long 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;

        long 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--;
        	}
        }

        long 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