- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 64 lines of Go from the credited upstream file 1230D.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 10func Sol1230D(reader io.Reader, writer io.Writer) {11 type pair struct {12 a, b int6413 }14 15 in := bufio.NewReader(reader)16 out := bufio.NewWriter(writer)17 defer out.Flush()18 19 var n int20 Fscan(in, &n)21 if n == 1 {22 Fprint(out, 0)23 return24 }25 arr := make([]pair, n)26 for i := range arr {27 Fscan(in, &arr[i].a)28 }29 for i := range arr {30 Fscan(in, &arr[i].b)31 }32 33 sort.Slice(arr, func(i, j int) bool { return arr[i].a < arr[j].a })34 multi := map[int64]bool{}35 for i := 0; i < n-1; i++ {36 if arr[i].a == arr[i+1].a {37 multi[arr[i].a] = true38 }39 }40 multiList := []int64{}41 for k := range multi {42 multiList = append(multiList, k)43 }44 ans := int64(0)45 for _, p := range arr {46 a, b := p.a, p.b47 if multi[a] {48 ans += b49 } else {50 for _, k := range multiList {51 if a&^k == 0 {52 ans += b53 break54 }55 }56 }57 }58 Fprint(out, ans)59}60 61626364