結果

問題 No.5 数字のブロック
コンテスト
ユーザー fmhr
提出日時 2015-05-14 11:56:15
言語 Go
(1.25.5)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 882 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 12,728 ms
コンパイル使用メモリ 238,496 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-01-02 00:38:12
合計ジャッジ時間 14,568 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package main

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

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

	var L, N int
	// fmt.Scanとbufio.Scannerを混ぜるとバッファリングの問題で正しく読めないため、nextIntに統一
	L = int(nextInt())
	N = int(nextInt())
	w := make([]int, N)
	for i := 0; i < N; i++ {
		w[i] = int(nextInt())
	}
	sort.Sort(sort.IntSlice(w))
	//	fmt.Println(w)
	z := 0
	ans := 0
	for _, x := range w {
		z += x
		if z > L {
			break
		} else {
			ans += 1
		}
	}
	fmt.Println(ans)
}

var s = bufio.NewScanner(os.Stdin)

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

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