結果

問題 No.297 カードの数式
ユーザー t8m8⛄️t8m8⛄️
提出日時 2016-01-25 18:28:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 145 ms / 1,000 ms
コード長 1,623 bytes
コンパイル時間 4,112 ms
コンパイル使用メモリ 87,864 KB
実行使用メモリ 57,468 KB
最終ジャッジ日時 2023-08-26 22:51:01
合計ジャッジ時間 8,272 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
56,692 KB
testcase_01 AC 142 ms
56,964 KB
testcase_02 AC 140 ms
57,468 KB
testcase_03 AC 141 ms
56,796 KB
testcase_04 AC 143 ms
56,932 KB
testcase_05 AC 141 ms
56,872 KB
testcase_06 AC 136 ms
57,044 KB
testcase_07 AC 140 ms
56,628 KB
testcase_08 AC 143 ms
56,920 KB
testcase_09 AC 142 ms
56,932 KB
testcase_10 AC 142 ms
56,836 KB
testcase_11 AC 140 ms
56,924 KB
testcase_12 AC 141 ms
54,840 KB
testcase_13 AC 139 ms
56,800 KB
testcase_14 AC 139 ms
56,940 KB
testcase_15 AC 144 ms
56,792 KB
testcase_16 AC 141 ms
56,768 KB
testcase_17 AC 140 ms
56,768 KB
testcase_18 AC 144 ms
57,192 KB
testcase_19 AC 143 ms
56,928 KB
testcase_20 AC 145 ms
56,756 KB
testcase_21 AC 145 ms
56,500 KB
testcase_22 AC 142 ms
57,064 KB
testcase_23 AC 141 ms
57,300 KB
testcase_24 AC 144 ms
56,860 KB
testcase_25 AC 142 ms
57,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class No0294 {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);

	static void solve() {
		int n = in.nextInt();
		int cnt = 0;
		ArrayList<Integer> num = new ArrayList<>();
		for (int i=0; i<n; i++) {
			char c = in.next().charAt(0);
			if (c == '-') cnt++;
			if (c == '+' || c == '-') continue;
			num.add(c - '0');
		}
		Collections.sort(num);

		int size = num.size();
		int nn = n - size + 1;

		long x = 0, dig = 1;
		for (int i=nn-1; i<size; i++) {
			x += num.get(i)*dig;
			dig *= 10;
		}

		long max = 0;
		int tmp = cnt;
		for (int i=0; i<nn-1; i++) {
			if (cnt > 0) {
				max -= num.get(i);
				cnt--;
			} else {
				max += num.get(i);
			}
		}
		max += x;

		cnt = tmp;
		long min = 0;
		if (cnt > 0) {
			min -= x;
			cnt--;

			for (int i=nn-2; i>=0; i--) {
				if (cnt > 0) {
					min -= num.get(i);
					cnt--;
				} else {
					min += num.get(i);
				}
			}
		} else {
			int[] a = new int[nn];
			dig = 1;
			for (int i=size-1,j=0; i>=0; i--, j++) {
				if (j == nn) {
					j = 0;
					dig *= 10;
				}
				a[j] += num.get(i)*dig;
			}
			// trace(a);

			for (int i=0; i<nn; i++) {
				min += a[i];
			}
		}

		out.println(max + " " + min);
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();

		solve();
		out.flush();

		long end = System.currentTimeMillis();
		//trace(end-start + "ms");
		in.close();
		out.close();
	}

	static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}
0