結果

問題 No.286 Modulo Discount Store
ユーザー yuki2006
提出日時 2015-07-09 20:24:54
言語 Go1.4
(1.4.2)
結果
TLE  
実行時間 -
コード長 1,400 bytes
コンパイル時間 201 ms
コンパイル使用メモリ 34,828 KB
実行使用メモリ 13,644 KB
最終ジャッジ日時 2024-11-25 00:22:17
合計ジャッジ時間 34,201 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 TLE * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func main() {

	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)
	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