結果

問題 No.297 カードの数式
ユーザー GrenacheGrenache
提出日時 2015-11-07 00:31:03
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,268 bytes
コンパイル時間 3,822 ms
コンパイル使用メモリ 75,308 KB
実行使用メモリ 56,280 KB
最終ジャッジ日時 2023-10-11 14:45:41
合計ジャッジ時間 8,406 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,748 KB
testcase_01 AC 125 ms
55,728 KB
testcase_02 AC 125 ms
56,032 KB
testcase_03 AC 125 ms
55,960 KB
testcase_04 AC 124 ms
55,976 KB
testcase_05 AC 125 ms
56,072 KB
testcase_06 WA -
testcase_07 AC 125 ms
55,688 KB
testcase_08 AC 126 ms
55,612 KB
testcase_09 AC 128 ms
55,668 KB
testcase_10 AC 128 ms
55,816 KB
testcase_11 AC 135 ms
55,552 KB
testcase_12 AC 127 ms
56,020 KB
testcase_13 AC 127 ms
55,988 KB
testcase_14 AC 126 ms
55,908 KB
testcase_15 AC 127 ms
56,164 KB
testcase_16 AC 128 ms
56,024 KB
testcase_17 AC 129 ms
55,772 KB
testcase_18 AC 128 ms
55,956 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 131 ms
54,160 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--;
        	}
        }

        if (m == 0) {
        	max = 0;
            for (int i = 0; i < n; i++) {
            	if (c[i] == '+') {
            	} else if (c[i] == '-') {
            	} else {
            		d[c[i] - '0']++;
            	}
            }

            long base = 1;
            int i = 0;
        	for (int j = 9; j >= 0; j--) {
        		while (d[j] > 0) {
        			max += base * j;
        			d[j]--;
            		i++;
            		if (i > p + m) {
            			i = 0;
            			base *= 10;
            		}
        		}
            }
        }
        
        System.out.printf("%d %d\n", max, min);

        sc.close();
    }
}
0