結果

問題 No.297 カードの数式
ユーザー t8m8⛄️
提出日時 2016-01-25 18:18:52
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,391 bytes
コンパイル時間 4,220 ms
コンパイル使用メモリ 93,264 KB
実行使用メモリ 42,692 KB
最終ジャッジ日時 2024-09-21 16:12:55
合計ジャッジ時間 9,648 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 7
権限があれば一括ダウンロードができます

ソースコード

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--;
		} else {
			min += x;
		}

		for (int i=nn-2; i>=0; i--) {
			if (cnt > 0) {
				min -= num.get(i);
				cnt--;
			} else {
				min += num.get(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