結果

問題 No.297 カードの数式
コンテスト
ユーザー ゴリポン先生
提出日時 2026-08-06 16:47:47
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 1 ms / 1,000 ms
+ 102µs
コード長 1,216 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,955 ms
コンパイル使用メモリ 196,224 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-08-06 16:47:54
合計ジャッジ時間 5,979 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

module main;
// https://yukicoder.me/submissions/74134 より
import std;
import std.ascii : isDigit;

void main()
{
	// 入力
	int N = readln.chomp.to!int;
	auto C = readln.split.join;
	// 答えの計算
	int[] num;
	int plus = 0, minus = 0;
	foreach (c; C) {
		if (isDigit(c)) num ~= c - '0';
		else if (c == '+') plus++;
		else minus++;
	}
	// 最大値
	num.sort;
	int[] num2 = num.dup;
	long max = 0;
	foreach (i; 0 .. minus) {
		max -= num2[0];
		num2.popFront;
	}
	foreach (i; 0 .. plus) {
		max += num2[0];
		num2.popFront;
	}
	long add = 0;
	foreach_reverse (n; num2) {
		add *= 10;
		add += n;
	}
	max += add;
	// 最小値
	long min = 0;
	if (minus == 0) {
		// - がない場合
		num2 = num.dup.reverse;
		auto pow10 = 1L.recurrence!((a,n) => a[n-1] * 10).take(16).array;
		foreach (i; 0 .. num2.length)
			min += num2[i] * pow10[i / (plus + 1)];
	} else {
		// - がある場合
		num2 = num.dup;
		foreach (i; 0 .. plus + 1) {
			min += num2[0];
			num2.popFront;
		}
		foreach (i; 0 .. minus - 1) {
			min -= num2[0];
			num2.popFront;
		}
		add = 0;
		foreach_reverse (i; 0 .. num2.length) {
			add *= 10;
			add += num2[i];
		}
		min -= add;
	}
	// 答えの出力
	writefln("%d %d", max, min);
}
0