- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 60 lines of Go from the credited upstream file 1316E.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "math/bits"8 "sort"9)10 1112func CF1316E(_r io.Reader, out io.Writer) {13 in := bufio.NewReader(_r)14 max := func(a, b int64) int64 {15 if a > b {16 return a17 }18 return b19 }20 type pair struct {21 v int22 w []int23 }24 25 var n, playerNum, audienceNum int26 Fscan(in, &n, &playerNum, &audienceNum)27 a := make([]pair, n)28 for i := range a {29 Fscan(in, &a[i].v)30 }31 for i := range a {32 a[i].w = make([]int, playerNum)33 for j := range a[i].w {34 Fscan(in, &a[i].w[j])35 }36 }37 38 sort.Slice(a, func(i, j int) bool { return a[i].v > a[j].v })39 40 dp := make([]int64, 1<<playerNum)41 for i := range dp {42 dp[i] = -1e1843 }44 dp[0] = 045 for i, p := range a {46 for s := 1<<playerNum - 1; s >= 0; s-- { 47 if bits.OnesCount8(uint8(s)) > i-audienceNum {48 dp[s] += int64(p.v) 49 }50 for t, lb := s, 0; t > 0; t ^= lb {51 lb = t & -t52 dp[s] = max(dp[s], dp[s^lb]+int64(p.w[bits.TrailingZeros8(uint8(lb))]))53 }54 }55 }56 Fprint(out, dp[1<<playerNum-1])57}58 5960