結果

問題 No.1858 Gorgeous Knapsack
ユーザー irumo8202irumo8202
提出日時 2022-02-28 20:17:40
言語 Go
(1.22.1)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,782 bytes
コンパイル時間 13,171 ms
コンパイル使用メモリ 209,084 KB
実行使用メモリ 4,336 KB
最終ジャッジ日時 2023-09-21 04:47:04
合計ジャッジ時間 15,525 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,332 KB
testcase_01 AC 1 ms
4,332 KB
testcase_02 AC 2 ms
4,332 KB
testcase_03 AC 2 ms
4,332 KB
testcase_04 AC 10 ms
4,328 KB
testcase_05 AC 3 ms
4,332 KB
testcase_06 AC 10 ms
4,332 KB
testcase_07 AC 6 ms
4,332 KB
testcase_08 AC 15 ms
4,332 KB
testcase_09 AC 25 ms
4,332 KB
testcase_10 AC 9 ms
4,332 KB
testcase_11 AC 13 ms
4,336 KB
testcase_12 AC 20 ms
4,332 KB
testcase_13 AC 4 ms
4,328 KB
testcase_14 AC 1 ms
4,332 KB
testcase_15 AC 1 ms
4,332 KB
testcase_16 AC 2 ms
4,328 KB
testcase_17 AC 1 ms
4,332 KB
testcase_18 AC 2 ms
4,332 KB
testcase_19 AC 1 ms
4,328 KB
testcase_20 AC 1 ms
4,328 KB
testcase_21 AC 1 ms
4,332 KB
testcase_22 AC 2 ms
4,336 KB
testcase_23 AC 2 ms
4,332 KB
testcase_24 AC 4 ms
4,328 KB
testcase_25 AC 16 ms
4,332 KB
testcase_26 AC 14 ms
4,332 KB
testcase_27 AC 13 ms
4,332 KB
testcase_28 AC 9 ms
4,336 KB
testcase_29 AC 2 ms
4,332 KB
testcase_30 AC 3 ms
4,332 KB
testcase_31 AC 2 ms
4,328 KB
testcase_32 AC 2 ms
4,332 KB
testcase_33 AC 3 ms
4,332 KB
testcase_34 AC 53 ms
4,332 KB
testcase_35 AC 26 ms
4,336 KB
testcase_36 AC 53 ms
4,328 KB
testcase_37 AC 1 ms
4,332 KB
testcase_38 AC 2 ms
4,328 KB
testcase_39 AC 1 ms
4,328 KB
testcase_40 AC 27 ms
4,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func print(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func slice_conv(x []int, sep string) string {
	ret := make([]string, len(x))
	for i, v := range x {
		ret[i] = strconv.Itoa(v)
	}
	return strings.Trim(strings.Join(ret, sep), "[]")
}

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

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

func i2() (int, int) {
	return nextInt(), nextInt()
}

func nextFloat() float64 {
	sc.Scan()
	i, err := strconv.ParseFloat(sc.Text(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

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

func nmax(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

func nmin(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

func abs(a int) int {
	return int(math.Abs(float64(a)))
}

const (
	MOD = 1000000007
	INF = 1 << 10
	M   = 100
)

func modAdd(a *int, b int) {
	*a += b
	if *a >= MOD {
		*a -= MOD
	}
}

func chmax(a *int, b int) {
	if *a < b {
		*a = b
	}
}

type Pair struct {
	v, w int
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)

	n, m := i2()

	items := make([]Pair, n)
	for range items {
		v, w := i2()
		items = append(items, Pair{v, w})
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i].v > items[j].v
	})

	dp := make([]int, m+1)
	ans := 0

	for _, p := range items {
		for i := m; i >= p.w; i-- {
			chmax(&dp[i], dp[i-p.w]+p.v)
		}
		chmax(&ans, dp[m]*p.v)
	}
	print(ans)
}
0