- 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
- 70 lines of Go from the credited upstream file 377C.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 CF377C(_r io.Reader, out io.Writer) {13 in := bufio.NewReader(_r)14 min := func(a, b int) int {15 if a < b {16 return a17 }18 return b19 }20 max := func(a, b int) int {21 if a > b {22 return a23 }24 return b25 }26 27 var n int28 Fscan(in, &n)29 a := make(IntSlice, n)30 for i := range a {31 Fscan(in, &a[i])32 }33 Sort(Reverse(a))34 Fscan(in, &n)35 op := make([]struct{ team, action string }, n)36 for i := n - 1; i >= 0; i-- {37 Fscan(in, &op[i].action, &op[i].team)38 }39 40 m := 1 << n41 dp := make([]int, m)42 for s := 1; s < m; s++ {43 op := op[bits.OnesCount(uint(s))-1]44 if op.team == "1" {45 dp[s] = -1e946 for t, lb := s, 0; t > 0; t ^= lb {47 lb = t & -t48 v := dp[s^lb]49 if op.action == "p" {50 v += a[bits.TrailingZeros(uint(lb))]51 }52 dp[s] = max(dp[s], v)53 }54 } else {55 dp[s] = 1e956 for t, lb := s, 0; t > 0; t ^= lb {57 lb = t & -t58 v := dp[s^lb]59 if op.action == "p" {60 v -= a[bits.TrailingZeros(uint(lb))]61 }62 dp[s] = min(dp[s], v)63 }64 }65 }66 Fprint(out, dp[m-1])67}68 6970