forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
453B.go
69 lines (65 loc) · 1.23 KB
/
453B.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
. "fmt"
"io"
)
// github.com/EndlessCheng/codeforces-go
func CF453B(in io.Reader, out io.Writer) {
primes := [...]int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}
const mx = len(primes)
const mx2 = 1 << mx
masks := [59]int{}
for i := 2; i < 59; i++ {
for j, p := range primes {
if i%p == 0 {
masks[i] |= 1 << j
}
}
}
abs := func(x int) int {
if x < 0 {
return -x
}
return x
}
var n, v int
Fscan(in, &n)
dp := make([][mx2]int, n+1)
for i := range dp {
for j := 0; j < mx2; j++ {
dp[i][j] = 1e9
}
}
dp[0][0] = 0
type pair struct{ j, v int }
fa := make([][mx2]pair, n+1)
for i := 1; i <= n; i++ {
Fscan(in, &v)
for j := 1; j == 1 || j < v*2-1; j++ {
m := masks[j]
c := m ^ (mx2 - 1)
for sub, ok := c, true; ok; ok = sub != c {
if s := dp[i-1][sub] + abs(j-v); s < dp[i][m|sub] {
dp[i][m|sub] = s
fa[i][m|sub] = pair{sub, j}
}
sub = (sub - 1) & c
}
}
}
miJ := 0
for j, v := range dp[n] {
if v < dp[n][miJ] {
miJ = j
}
}
ans := make([]int, n)
for i, j := n, miJ; i > 0; i-- {
ans[i-1] = fa[i][j].v
j = fa[i][j].j
}
for _, v := range ans {
Fprint(out, v, " ")
}
}
//func main() { CF453B(os.Stdin, os.Stdout) }