結果

問題 No.286 Modulo Discount Store
コンテスト
ユーザー yuki2006
提出日時 2015-07-09 20:24:54
言語 Go
(1.25.5)
結果
TLE  
実行時間 -
コード長 1,576 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 14,184 ms
コンパイル使用メモリ 257,720 KB
実行使用メモリ 16,208 KB
最終ジャッジ日時 2026-01-02 11:58:16
合計ジャッジ時間 18,378 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other AC * 2 TLE * 1 -- * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package main

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

func main() {
	// Scan開始前にSplitを設定しないとPanicになるため、ここに移動
	s.Split(bufio.ScanWords)

	N := nextInt()
	var data = make([]int, N)

	for i := 0; i < N; i++ {
		data[i] = nextInt()
	}

	sort.Ints(data)

	minBuyTotal := N * 10000
	for {
		buy := 0
		priceTotal := 0
		for i := 0; i < N; i++ {
			buy += data[i] - min(data[i], priceTotal%1000)
			priceTotal += data[i]
		}
		minBuyTotal = min(minBuyTotal, buy)
		if !next_permutation(data) {
			break
		}
	}
	fmt.Println(minBuyTotal)

}

func next_permutation(a []int) bool {
	for i := len(a) - 2; i >= 0; i-- {
		if a[i] < a[i+1] {
			for j := len(a) - 1; j >= 0; j-- {
				if a[j] >= a[i] {
					a[i], a[j] = a[j], a[i]
					reverse(a, i+1, len(a)-(i+1))
					return true
				}
			}
		}
	}
	reverse(a, 0, len(a))

	return false
}
func reverse(a []int, start int, length int) {
	var end = start + length - 1
	for start < end {
		a[start], a[end] = a[end], a[start]
		start++
		end--
	}
}

var s = bufio.NewScanner(os.Stdin)

func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func next() string {
	// s.Split(bufio.ScanWords) // 削除: Scan後に呼ぶとPanicになるため
	s.Scan()
	return s.Text()
}
func nextLine() string {
	// s.Split(bufio.ScanLines) // 削除: 同上
	s.Scan()
	return s.Text()
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}
func nextLong() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}
0