結果

問題 No.617 Nafmo、買い出しに行く
ユーザー moti
提出日時 2018-05-20 15:39:59
言語 Go
(1.23.4)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 807 bytes
コンパイル時間 14,417 ms
コンパイル使用メモリ 227,184 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-28 14:41:42
合計ジャッジ時間 15,040 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

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

func main() {
	cols := strings.Split(nextLine(), " ")
	n, err := strconv.Atoi(cols[0])
	if err != nil {
		log.Fatal(err)
	}
	k, err := strconv.Atoi(cols[1])
	if err != nil {
		log.Fatal(err)
	}
	var a []int
	for i := 0; i < n; i++ {
		m := nextInt()
		a = append(a, m)
	}
	ma := 0
	for s := 0; s < (1 << uint(n)); s++ {
		count := 0
		for i := 0; i < n; i++ {
			if (s & (1 << uint(i))) > 0 {
				count += a[i]
			}
		}
		if count <= k {
			ma = int(math.Max(float64(ma), float64(count)))
		}
	}
	fmt.Println(ma)
}
0