結果
| 問題 |
No.297 カードの数式
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-11-06 15:35:07 |
| 言語 | Python2 (2.7.18) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,542 bytes |
| コンパイル時間 | 582 ms |
| コンパイル使用メモリ | 7,040 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-13 13:04:10 |
| 合計ジャッジ時間 | 1,307 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 15 WA * 8 |
ソースコード
# -*- 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)
for i in xrange(len(opes)):
if opes[i] == "+":
total += nums[N - i - 1]
else:
total -= nums[N - i - 1]
return total
def minValue(nums, opes):
# -が1文字でもある場合
if "-" in opes:
# 並び替えて-優先にする
opes.reverse()
N = len(nums)
total = -bestChoiceValue(nums, opes)
for i in xrange(len(opes)):
if opes[i] == "-":
total += nums[N - i - 1]
else:
total -= nums[N - i - 1]
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