結果

問題 No.297 カードの数式
ユーザー GrenacheGrenache
提出日時 2015-11-07 00:17:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,377 bytes
コンパイル時間 3,407 ms
コンパイル使用メモリ 74,464 KB
実行使用メモリ 56,400 KB
最終ジャッジ日時 2023-10-11 14:38:30
合計ジャッジ時間 7,946 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,908 KB
testcase_01 AC 123 ms
56,136 KB
testcase_02 AC 124 ms
56,072 KB
testcase_03 AC 124 ms
55,768 KB
testcase_04 AC 123 ms
55,584 KB
testcase_05 AC 123 ms
56,052 KB
testcase_06 WA -
testcase_07 AC 123 ms
56,176 KB
testcase_08 AC 124 ms
55,712 KB
testcase_09 AC 123 ms
55,892 KB
testcase_10 AC 123 ms
56,084 KB
testcase_11 AC 123 ms
55,732 KB
testcase_12 AC 123 ms
55,488 KB
testcase_13 AC 125 ms
55,852 KB
testcase_14 AC 124 ms
55,928 KB
testcase_15 AC 123 ms
55,380 KB
testcase_16 AC 124 ms
56,176 KB
testcase_17 AC 126 ms
55,508 KB
testcase_18 AC 128 ms
55,904 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 123 ms
56,180 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) {
            for (int i = 0; i < n; i++) {
            	if (c[i] == '+') {
            	} else if (c[i] == '-') {
            	} else {
            		d[c[i] - '0']++;
            	}
            }

            num = new long[p + m + 1];
            int i = p + m;
            for (int j = 0; j < 10; j++) {
            	while (d[j] > 0) {
            		num[i] = num[i] * 10 + j;
            		d[j]--;
            		i--;
            		if (i < 0) {
            			i = p + m;
            		}
            	}
            }

            max = 0;
            for (int j = 0; j < p + m + 1; j++) {
            	max += num[j];
            }
        }
        
        System.out.printf("%d %d\n", max, min);

        sc.close();
    }
}
0