Approach
Sorting and greedy selection
For Codeforces 884F — Anti-Palindromize, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 53 lines of Go from the credited upstream file 884F.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
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 . "fmt"5 "io"6 "slices"7)8 910func cf884F(in io.Reader, out io.Writer) {11 var n, ans, totC int12 var s string13 Fscan(in, &n, &s)14 b := make([]int, n)15 for i := range b {16 Fscan(in, &b[i])17 ans += b[i]18 }19 20 cnt := [26]int{}21 for i := range n / 2 {22 if s[i] == s[n-1-i] {23 cnt[s[i]-'a']++24 totC++25 ans -= min(b[i], b[n-1-i])26 }27 }28 29 maxC, maxCh := 0, byte(0)30 for ch, c := range cnt {31 if c > maxC {32 maxC, maxCh = c, 'a'+byte(ch)33 }34 }35 36 if maxC*2 > totC {37 a := []int{}38 for i := range n / 2 {39 if s[i] != maxCh && s[n-1-i] != maxCh && s[i] != s[n-1-i] {40 a = append(a, min(b[i], b[n-1-i]))41 }42 }43 slices.Sort(a)44 for _, v := range a[:maxC*2-totC] {45 ans -= v46 }47 }48 49 Fprint(out, ans)50}51 5253