- 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
- 64 lines of Go from the credited upstream file 808E.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 "sort"8)9 1011func CF808E(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 max := func(a, b int64) int64 {14 if b > a {15 return b16 }17 return a18 }19 20 var n, maxW, w, v int21 Fscan(in, &n, &maxW)22 a := [3]sort.IntSlice{}23 for ; n > 0; n-- {24 Fscan(in, &w, &v)25 a[w-1] = append(a[w-1], v)26 }27 for _, b := range a {28 sort.Sort(sort.Reverse(b))29 }30 31 32 33 type pair struct {34 x, y int35 res int6436 }37 dp := make([]pair, maxW+1)38 for i := 1; i <= maxW; i++ {39 dp[i] = dp[i-1]40 if dp[i].x < len(a[0]) {41 dp[i].res += int64(a[0][dp[i].x])42 dp[i].x++43 }44 if i > 1 && dp[i-2].y < len(a[1]) && dp[i-2].res+int64(a[1][dp[i-2].y]) > dp[i].res {45 dp[i] = dp[i-2]46 dp[i].res += int64(a[1][dp[i-2].y])47 dp[i].y++48 }49 }50 51 ans := dp[maxW].res52 s3 := int64(0)53 for i, v := range a[2] {54 if (i+1)*3 > maxW {55 break56 }57 s3 += int64(v)58 ans = max(ans, s3+dp[maxW-(i+1)*3].res)59 }60 Fprint(out, ans)61}62 6364