- 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
- 72 lines of Go from the credited upstream file 1183F.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 1011func CF1183F(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 max := func(a, b int) int {16 if a > b {17 return a18 }19 return b20 }21 22 var t, n int23 for Fscan(in, &t); t > 0; t-- {24 Fscan(in, &n)25 a := make([]int, n)26 for i := range a {27 Fscan(in, &a[i])28 }29 30 31 sort.Ints(a)32 j := 033 for i := 1; i < n; i++ {34 if a[j] != a[i] {35 j++36 a[j] = a[i]37 }38 }39 ans := a[j]40 n = j + 141 a = a[:n]42 43 44 for i := n - 1; i >= 0; i-- {45 ai := a[i]46 cnt := 047 for j := i - 1; j >= 0; j-- {48 aj := a[j]49 if ai%aj != 0 {50 ans = max(ans, ai+aj) 51 cnt2 := 052 for k := j - 1; k >= 0; k-- {53 ak := a[k]54 if ai%ak != 0 && aj%ak != 0 {55 ans = max(ans, ai+aj+ak) 56 if cnt2++; cnt2 == 10 {57 break58 }59 }60 }61 if cnt++; cnt == 10 {62 break63 }64 }65 }66 }67 Fprintln(out, ans)68 }69}70 7172