結果

問題 No.297 カードの数式
ユーザー yukirinyukirin
提出日時 2016-02-28 19:29:49
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,289 bytes
コンパイル時間 11,646 ms
コンパイル使用メモリ 207,800 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 22:53:18
合計ジャッジ時間 12,712 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	var ns []int
	var ss []string
	mC, zC := 0, 0

	for i := 0; i < n; i++ {
		w := nextLine()
		if w == "+" || w == "-" {
			if w == "-" {
				mC++
			}

			ss = append(ss, w)
			continue
		}
		num, _ := strconv.Atoi(w)

		if num == 0 {
			zC++
		}
		ns = append(ns, num)
	}
	sort.Ints(ns)
	sort.Strings(ss)
	big := ns[len(ns)-1]
	for i := len(ns) - 2; i >= len(ss); i-- {
		big *= 10
		big += ns[i]
	}

	max := big
	for i, ope := range ss {
		nI := len(ss) - 1 - i
		switch ope {
		case "+":
			max += ns[nI]
		case "-":
			max -= ns[nI]
		}
	}

	if mC == 0 {
		minS := make([]int, len(ss)+1)
		for i, v := range ns {
			m := len(minS)
			minS[i%m] *= 10
			minS[i%m] += v
		}
		min := 0
		for _, v := range minS {
			min += v
		}
		fmt.Println(max, min)
	} else {
		min := ns[0]
		ns[len(ss)] = big
		ns = ns[1:]
		for i, ope := range ss {
			switch ope {
			case "+":
				min += ns[i]
			case "-":
				min -= ns[i]
			}
		}
		fmt.Println(max, min)
	}
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, _ := strconv.Atoi(nextLine())
	return i
}
0