結果

問題 No.297 カードの数式
ユーザー yuki2006yuki2006
提出日時 2015-11-06 16:21:34
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 1,798 bytes
コンパイル時間 61 ms
コンパイル使用メモリ 6,664 KB
実行使用メモリ 6,100 KB
最終ジャッジ日時 2023-10-11 14:22:33
合計ジャッジ時間 1,367 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,856 KB
testcase_01 AC 11 ms
5,852 KB
testcase_02 AC 11 ms
5,856 KB
testcase_03 AC 11 ms
6,028 KB
testcase_04 AC 11 ms
5,912 KB
testcase_05 AC 10 ms
5,852 KB
testcase_06 AC 11 ms
5,856 KB
testcase_07 AC 10 ms
5,900 KB
testcase_08 AC 11 ms
5,992 KB
testcase_09 AC 11 ms
5,904 KB
testcase_10 AC 10 ms
6,100 KB
testcase_11 WA -
testcase_12 AC 10 ms
6,000 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 11 ms
5,860 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 10 ms
5,980 KB
testcase_19 AC 10 ms
5,992 KB
testcase_20 AC 11 ms
5,972 KB
testcase_21 AC 11 ms
5,852 KB
testcase_22 AC 11 ms
5,904 KB
testcase_23 AC 11 ms
5,852 KB
testcase_24 AC 11 ms
5,892 KB
testcase_25 AC 11 ms
5,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

N = int(raw_input())

nums = []
opes = []
for l in raw_input().split():
    if l.isdigit():
        nums.append(int(l))
    elif l in ["+", "-"]:
        opes.append(l)
    else:
        raise Exception()

nums.sort(reverse=True)
# + -> - にならべる
opes.sort()


def bestChoiceValue(nums, opes):
    N = len(nums)
    return int("".join(map(str, nums[:N - len(opes)])))


def maxValue(nums, opes):
    N = len(nums)

    total = bestChoiceValue(nums, opes)
    k = len(nums) - len(opes)
    
    for i in xrange(len(opes)):

        if opes[i] == "+":
            total += nums[k + i]
        else:
            total -= nums[k + i]
    return total


def minValue(nums, opes):
    # -が1文字でもある場合
    if "-" in opes:
        # 並び替えて- -> + 優先にする
        opes.reverse()
        total = -bestChoiceValue(nums, opes)

        k = len(nums) - len(opes)
        for i in xrange(len(opes)):
            if opes[i] == "+":
                total += nums[k + i]
            else:
                if i == len(opes) - 1:
                    # 一番小さな値は先頭に持ってきてプラスにする
                    total += nums[k + i]
                else:
                    total -= nums[k + i]
        return total
    else:
        # -がない場合
        i = 0
        digit = 1
        total = 0
        # 最小値を作るために大きい物から順に小さい桁に入れていく。
        while i < len(nums):
            j = 0
            while j < len(opes) + 1 and i < len(nums):
                total += nums[i] * digit
                i += 1
                j += 1
            # 次の桁へ
            digit *= 10
        return total


mx = maxValue(nums, opes)
mn = minValue(nums, opes)

print mx, mn
0