結果

問題 No.297 カードの数式
ユーザー t8m8⛄️
提出日時 2016-01-25 18:28:00
言語 Java
(openjdk 23)
結果
AC  
実行時間 200 ms / 1,000 ms
コード長 1,623 bytes
コンパイル時間 4,637 ms
コンパイル使用メモリ 86,920 KB
実行使用メモリ 42,588 KB
最終ジャッジ日時 2024-12-26 02:25:14
合計ジャッジ時間 11,017 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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