結果

問題 No.297 カードの数式
ユーザー spacia
提出日時 2015-12-14 22:59:31
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,738 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 84,672 KB
実行使用メモリ 40,780 KB
最終ジャッジ日時 2024-09-15 12:45:31
合計ジャッジ時間 6,201 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main {

	public static long getMin (int[] nums , int addCnt , int subCnt) {
		long ret = nums[0];
		int concatCnt = 0 , len = nums.length;
		Arrays.sort(nums);
		int ai = 0, si = 0;
		for (int i = 1; i < len; i++) {
			if (addCnt + subCnt > 1 && addCnt > 0) {
				ret += nums[i];
				addCnt--;
			} else if (addCnt + subCnt > 1 && subCnt > 0) {
				ret -= nums[i];
				subCnt--;
			} else {
				String str = "";
				for (int j = i; j < len; j++) {
					str = (nums[j] +  "") + str;
				}
				ret = addCnt > 0 ? ret + Long.parseLong(str) : ret - Long.parseLong(str);
				break;
			}
		}
		return ret;
	}

	public static long getMax (int[] nums , int addCnt , int subCnt) {
		long ret = 0;
		int concatCnt = 0 , len = nums.length;
		Arrays.sort(nums);
		int ai = 0, si = 0;
		for (int i = len - 1; i >= 0; i--) {
			if (len - concatCnt++ > addCnt + subCnt) {
				ret = ret * 10 + nums[i];
				continue;
			} else if (ai++ < addCnt) {
				ret += nums[i];
			} else if (si < subCnt) {
				ret -= nums[i];
			}
		}
		return ret;
	}
	
	public static void main (String[] args) throws IOException {
		BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in));

		int addCnt = 0, subCnt = 0;
		int n = Integer.parseInt(br.readLine());
		String[] str = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			if (str[i].equals("+")) {
				addCnt++;
			} else if (str[i].equals("-")) {
				subCnt++;
			}
		}
		int[] nums = new int[n - (addCnt + subCnt)];
		int j = 0;
		for (String s : str) {
			if (!s.equals("+") && !s.equals("-")) {
				nums[j++] = Integer.parseInt(s);
			}
		}
		System.out.println(getMax(nums , addCnt , subCnt) + " " + getMin(nums , addCnt , subCnt));
	}
}
0