{"unit":"L-pcx-409","statement":"∃ C : ℕ, 0 < C ∧ ∀ (v g : ℕ) (a b z : Fin g → Fin v),\n  let X := Fin v → Bool\n  let add := fun (s t : Bool) => decide (s ≠ t)\n  let bad := fun (x : X) => (Finset.univ.filter (fun j : Fin g =>\n    x (z j) ≠ !(x (a j) && x (b j)))).card\n  ∃ n : ℕ, n ≤ C*(v+g+1) ∧\n    let V := Bool × Fin n\n    let Y := V → Bool\n    ∃ (σ τ : Equiv.Perm (Fin n)) (mark : (Fin (v+1) × Bool) ↪ V)\n        (enc : X → Bool → Y) (dec : Y → X × Bool),\n      let cost := fun (y : Y) =>\n        (Finset.univ.filter (fun i : Fin n => y (false,i) ≠ y (true,i))).card +\n        (Finset.univ.filter (fun i : Fin n => y (false,i) ≠ y (true,σ i))).card +\n        (Finset.univ.filter (fun i : Fin n => y (false,i) = y (true,τ i))).card\n      (∀ q, (mark q).1 = false) ∧\n      (∀ (x : X) (r : Bool), dec (enc x r) = (x,r) ∧\n        (∀ s : Bool, enc x r (mark (0,s)) = add r s) ∧\n        (∀ (i : Fin v) (s : Bool), enc x r (mark (i.succ,s)) = add (add (x i) r) s) ∧\n        (Finset.univ.filter (fun p : V => enc x r p = true)).card = n ∧\n        cost (enc x r) = 6*g + 2*bad x) ∧\n      (∀ y : Y, (Finset.univ.filter (fun p : V =>\n        y p ≠ enc (dec y).1 (dec y).2 p)).card + C*(6*g + 2*bad (dec y).1) ≤ C*cost y) ∧\n      ∀ pins : Fin v → Option Bool,\n        (Finset.univ.filter (fun y : Y => cost y = 6*g ∧ y (mark (0,false)) = false ∧\n          ∀ (i : Fin v) (s : Bool), pins i = some s → y (mark (i.succ,s)) = false)).card =\n        (Finset.univ.filter (fun x : X => bad x = 0 ∧\n          ∀ (i : Fin v) (s : Bool), pins i = some s → x i = s)).card","statement_sha256":"69151cdb05844f30ea45435c96160427810a1fb31e54300717e5b23bf8ddca7a","complete":true,"lean":"import Mathlib\nimport PEqualNP.Defs\nnamespace PEqualNP.Outside.L_pcx_409\nopen Finset\nopen scoped BigOperators\ndef diff (a b : Bool) : ℕ := if a ≠ b then 1 else 0\ntheorem diff_tri (a b c : Bool) : diff a c ≤ diff a b + diff b c := by\n  cases a <;> cases b <;> cases c <;> decide\ntheorem diff_symm (a b : Bool) : diff a b = diff b a := by\n  cases a <;> cases b <;> rfl\ntheorem diff_zero (a b : Bool) : diff a b = 0 ↔ a = b := by\n  cases a <;> cases b <;> decide\ntheorem diff_le (a b : Bool) : diff a b ≤ 1 := by\n  cases a <;> cases b <;> decide\ntheorem diff_self (a : Bool) : diff a a = 0 := (diff_zero _ _).2 rfl\ntheorem diff_xor (a b r : Bool) : diff (xor a r) (xor b r) = diff a b := by\n  cases a <;> cases b <;> cases r <;> rfl\ntheorem xor_cancel (a r : Bool) : xor (xor a r) r = a := by\n  cases a <;> cases r <;> rfl\ndef triangle (a b c : Bool) : ℕ :=\n  diff a (!b) + diff b (!c) + diff c (!a)\ntheorem choose_ratio (n t k : ℕ) (ht : 4 * t ≤ 3 * n) (hk : k ≤ t) :\n    4^k * t.choose k ≤ 3^k * n.choose k := by\n  have desc : 4^k * t.descFactorial k ≤ 3^k * n.descFactorial k := by\n    induction k with\n    | zero => simp\n    | succ k ih =>\n      rw [Nat.descFactorial_succ, Nat.descFactorial_succ, pow_succ, pow_succ]\n      calc\n        _ = (4^k * t.descFactorial k) * (4 * (t-k)) := by ring\n        _ ≤ (3^k * n.descFactorial k) * (3 * (n-k)) :=\n          Nat.mul_le_mul (ih (by omega)) (by omega)\n        _ = _ := by ring\n  rw [Nat.descFactorial_eq_factorial_mul_choose, Nat.descFactorial_eq_factorial_mul_choose] at desc\n  exact Nat.le_of_mul_le_mul_left (by simpa only [mul_left_comm] using desc) (Nat.factorial_pos _)\ntheorem card_large_subsets {A : Type} [Fintype A] [DecidableEq A]\n    (s t : ℕ) (hst : s ≤ t) (hts : t ≤ 2*s) :\n    (Fintype.card A).choose t ≤ ((Fintype.card A).choose s)^2 := by\n  let U := (univ : Finset A).powersetCard s\n  let V := (univ : Finset A).powersetCard t\n  have h : V.card ≤ (U ×ˢ U).card := by\n    apply card_le_card_of_surjOn (fun p : Finset A × Finset A => p.1 ∪ p.2)\n    intro T hT\n    have hcard := (mem_powersetCard.mp hT).2\n    obtain ⟨S,hST,hS⟩ := exists_subset_card_eq (s := T) (n := s) (by omega)\n    obtain ⟨R,hSR,hRT,hR⟩ := exists_subsuperset_card_eq\n      (s := T \\ S) (t := T) (n := s)\n      (sdiff_subset) (by rw [card_sdiff_of_subset hST]; omega) (by omega)\n    refine ⟨(S,R), mem_product.mpr ⟨mem_powersetCard.mpr ⟨subset_univ _,hS⟩,\n      mem_powersetCard.mpr ⟨subset_univ _,hR⟩⟩, ?_⟩\n    apply Subset.antisymm (union_subset hST hRT)\n    intro x hx\n    by_cases hxs : x ∈ S\n    · exact mem_union_left _ hxs\n    · exact mem_union_right _ (hSR (mem_sdiff.mpr ⟨hx,hxs⟩))\n  simpa [U,V,card_powersetCard,pow_two] using h\ndef images {A : Type} [DecidableEq A] (S T : Finset A) [Fintype A] :=\n  (univ : Finset (Equiv.Perm A)).filter (fun p => S.map p.toEmbedding = T)\ntheorem images_card {A : Type} [Fintype A] [DecidableEq A]\n    (S T : Finset A) (h : S.card = T.card) :\n    (images S T).card = (images S S).card := by\n  obtain ⟨p,hp⟩ := Equiv.Perm.exists_map_finset_eq T S h.symm\n  refine card_equiv (Equiv.mulLeft p) (fun q => ?_)\n  simp only [images,mem_filter,mem_univ,true_and]\n  have hm : S.map (p * q).toEmbedding = (S.map q.toEmbedding).map p.toEmbedding := by\n    rw [Finset.map_map]\n    rfl\n  change S.map q.toEmbedding = T ↔ S.map (p * q).toEmbedding = S\n  rw [hm]\n  exact ⟨fun e => e ▸ hp, fun e => Finset.map_injective p.toEmbedding (e.trans hp.symm)⟩\ndef contained {A : Type} [Fintype A] [DecidableEq A] (S T : Finset A) :=\n  (univ : Finset (Equiv.Perm A)).filter (fun p => S.map p.toEmbedding ⊆ T)\ntheorem contained_card {A : Type} [Fintype A] [DecidableEq A]\n    (S T : Finset A) :\n    (contained S T).card = T.card.choose S.card * (images S S).card := by\n  have hh := sum_card_fiberwise_eq_card_filter\n    (univ : Finset (Equiv.Perm A)) (T.powersetCard S.card) (fun p => S.map p.toEmbedding)\n  rw [← show (univ.filter fun p : Equiv.Perm A => S.map p.toEmbedding ∈\n      T.powersetCard S.card) = contained S T by\n    ext p\n    simp [contained,mem_powersetCard]] at *\n  rw [← hh]\n  change (∑ U ∈ T.powersetCard S.card, (images S U).card) = _\n  rw [sum_congr rfl (fun U hU => images_card S U ((mem_powersetCard.mp hU).2.symm))]\n  simp [card_powersetCard]\ntheorem perm_card_factor {A : Type} [Fintype A] [DecidableEq A] (S : Finset A) :\n    Fintype.card (Equiv.Perm A) =\n      (Fintype.card A).choose S.card * (images S S).card := by\n  simpa [contained] using contained_card S (univ : Finset A)\ntheorem geom_bound (n : ℕ) :\n    (∑ i ∈ range n, (1/2 : ℚ)^(i+1)) = 1 - (1/2 : ℚ)^n := by\n  induction n with\n  | zero => simp\n  | succ n ih =>\n    rw [sum_range_succ, ih, pow_succ (1/2 : ℚ) n]\n    ring\ntheorem ratio_bound (n s t : ℕ) (hs : s ≤ t) (ht : t ≤ 2*s)\n    (h : 4*t ≤ 3*n) (hsn : s ≤ n) :\n    (n.choose t : ℚ) * (t.choose s : ℚ) *\n      ((t.choose s : ℚ) / n.choose s)^31 ≤ (1/2 : ℚ)^s := by\n  have B : (0 : ℚ) < n.choose s := by exact_mod_cast Nat.choose_pos hsn\n  have A : (0 : ℚ) ≤ t.choose s := by positivity\n  have R : (t.choose s : ℚ) / n.choose s ≤ (3/4 : ℚ)^s := by\n    rw [div_le_iff₀ B, div_pow, div_mul_eq_mul_div, le_div_iff₀ (by positivity)]\n    exact_mod_cast (by simpa [mul_comm] using choose_ratio n t s h hs)\n  have T : (t.choose s : ℚ) ≤ (4 : ℚ)^s := by\n    exact_mod_cast calc\n      t.choose s ≤ 2^t := Nat.choose_le_two_pow _ _\n      _ ≤ 2^(2*s) := Nat.pow_le_pow_right (by decide) ht\n      _ = 4^s := by rw [pow_mul]; rfl\n  have N : (n.choose t : ℚ) ≤ (n.choose s : ℚ)^2 := by\n    have hN := card_large_subsets (A := Fin n) s t hs ht\n    simp only [Fintype.card_fin] at hN\n    exact_mod_cast hN\n  calc\n    _ ≤ (n.choose s : ℚ)^2 * (t.choose s : ℚ) *\n        ((t.choose s : ℚ) / n.choose s)^31 := by gcongr\n    _ = (t.choose s : ℚ)^3 * ((t.choose s : ℚ) / n.choose s)^29 := by\n      field_simp <;> ring\n    _ ≤ ((4 : ℚ)^s)^3 * (((3/4 : ℚ)^s)^29) := by gcongr\n    _ = ((4 : ℚ)^3 * (3/4 : ℚ)^29)^s := by\n      rw [mul_pow, ← pow_mul, ← pow_mul, ← pow_mul, ← pow_mul, Nat.mul_comm 3, Nat.mul_comm 29]\n    _ ≤ (1/2 : ℚ)^s := by gcongr <;> norm_num\ntheorem bad_size {A : Type} [Fintype A] [DecidableEq A] (s : ℕ)\n    (hs : 0 < s) (hN : 2*s ≤ Fintype.card A) :\n    let t := s + s/2\n    let bad := fun S T : Finset A =>\n      Fintype.piFinset (fun _ : Fin 32 => contained S T)\n    let B := ((univ : Finset A).powersetCard s).biUnion\n      (fun S => ((univ : Finset A).powersetCard t).biUnion (bad S))\n    (B.card : ℚ) / Fintype.card (Fin 32 → Equiv.Perm A) ≤ (1/2 : ℚ)^s := by\n  intro t bad B\n  let n := Fintype.card A\n  let M := Fintype.card (Fin 32 → Equiv.Perm A)\n  have Mpos : (0 : ℚ) < M := by\n    haveI : Nonempty (Fin 32 → Equiv.Perm A) := ⟨fun _ => Equiv.refl _⟩\n    exact_mod_cast Fintype.card_pos (α := Fin 32 → Equiv.Perm A)\n  have na : (0 : ℚ) < n.choose s := by exact_mod_cast Nat.choose_pos (by omega : s ≤ n)\n  have hcard (S T : Finset A) (hS : S.card = s) (hT : T.card = t) :\n      ((bad S T).card : ℚ) / M = ((t.choose s : ℚ) / n.choose s)^32 := by\n    have hc := contained_card S T\n    rw [hS,hT] at hc\n    have hp := perm_card_factor S\n    rw [hS] at hp\n    have hP : 0 < (Fintype.card (Equiv.Perm A) : ℚ) := by\n      haveI : Nonempty (Equiv.Perm A) := ⟨Equiv.refl _⟩\n      exact_mod_cast Fintype.card_pos (α := Equiv.Perm A)\n    have hF : 0 < ((images S S).card : ℚ) := by\n      have hh : 0 < Fintype.card (Equiv.Perm A) := by exact_mod_cast hP\n      rw [hp] at hh\n      exact_mod_cast Nat.pos_of_mul_pos_left hh\n    simp only [bad,Fintype.card_piFinset,prod_const,card_univ,Fintype.card_fin,hc,\n      M,Fintype.card_fun, Nat.cast_pow, Nat.cast_mul, ← div_pow]\n    congr 1\n    have hpq : (Fintype.card (Equiv.Perm A) : ℚ) =\n        (n.choose s : ℚ) * (images S S).card := by exact_mod_cast hp\n    rw [hpq]\n    field_simp\n  have unionBound {ι : Type} (U : Finset ι) (V : ι → Finset (Fin 32 → Equiv.Perm A)) :\n      ((U.biUnion V).card : ℚ) / M ≤ ∑ i ∈ U, ((V i).card : ℚ) / M := by\n    rw [← sum_div]\n    apply div_le_div_of_nonneg_right _ Mpos.le\n    exact_mod_cast card_biUnion_le\n  calc\n    _ ≤ ∑ S ∈ (univ : Finset A).powersetCard s,\n        ∑ T ∈ (univ : Finset A).powersetCard t, ((bad S T).card : ℚ) / M :=\n      (unionBound _ _).trans (sum_le_sum (fun _ _ => unionBound _ _))\n    _ = (n.choose s : ℚ) * (n.choose t : ℚ) * ((t.choose s : ℚ) / n.choose s)^32 := by\n      rw [sum_congr rfl (fun S hS => sum_congr rfl (fun T hT =>\n        hcard S T (mem_powersetCard.mp hS).2 (mem_powersetCard.mp hT).2))]\n      simp [card_powersetCard,n,mul_assoc]\n    _ ≤ _ := by\n      rw [mul_comm (n.choose s : ℚ) (n.choose t : ℚ)]\n      have ht : 4 * t ≤ 3 * n := by dsimp [t]; omega\n      let w : ℚ := (t.choose s : ℚ) / n.choose s\n      have he : (n.choose s : ℚ) * w = t.choose s := by\n        dsimp [w]\n        field_simp\n      calc\n        _ = (n.choose t : ℚ) * ((n.choose s : ℚ) * w) * w^31 := by\n          change (n.choose t : ℚ) * n.choose s * w^32 = _\n          rw [show 32 = 31+1 by decide,pow_succ]\n          ring\n        _ ≤ _ := by\n          rw [he]\n          exact ratio_bound n s t (by dsimp [t]; omega) (by dsimp [t]; omega) ht (by omega)\ntheorem expand_sets {A : Type} [Fintype A] [DecidableEq A] :\n    ∃ p : Fin 32 → Equiv.Perm A, ∀ S : Finset A, 2*S.card ≤ Fintype.card A →\n      S.card ≤ 2 * ∑ j : Fin 32, (S.filter (fun i => p j i ∉ S)).card := by\n  classical\n  let n := Fintype.card A\n  let M := Fintype.card (Fin 32 → Equiv.Perm A)\n  let B := fun i : ℕ =>\n    ((univ : Finset A).powersetCard (i+1)).biUnion (fun S =>\n      ((univ : Finset A).powersetCard (i+1+(i+1)/2)).biUnion (fun T =>\n        Fintype.piFinset (fun _ : Fin 32 => contained S T)))\n  let F := (range (n/2)).biUnion B\n  have Mpos : (0 : ℚ) < M := by\n    haveI : Nonempty (Fin 32 → Equiv.Perm A) := ⟨fun _ => Equiv.refl _⟩\n    exact_mod_cast Fintype.card_pos (α := Fin 32 → Equiv.Perm A)\n  have hF : (F.card : ℚ) / M < 1 := by\n    calc\n      _ ≤ ∑ i ∈ range (n/2), ((B i).card : ℚ) / M := by\n        rw [← sum_div]\n        exact div_le_div_of_nonneg_right (by exact_mod_cast card_biUnion_le) Mpos.le\n      _ ≤ ∑ i ∈ range (n/2), (1/2 : ℚ)^(i+1) := by\n        refine sum_le_sum fun i hi => ?_\n        have hi' := mem_range.mp hi\n        exact bad_size (A := A) (i+1) (by omega) (by omega)\n      _ < _ := by rw [geom_bound]; exact sub_lt_self _ (by positivity)\n  have hn : F ≠ univ := by\n    intro h\n    rw [h,card_univ,show Fintype.card (Fin 32 → Equiv.Perm A) = M from rfl,\n      div_self Mpos.ne'] at hF\n    exact (lt_irrefl 1 hF)\n  obtain ⟨p,hp⟩ : ∃ p, p ∉ F := by\n    by_contra! h\n    exact hn (eq_univ_iff_forall.mpr h)\n  refine ⟨p,fun S hS => ?_⟩\n  by_contra! hL\n  let s := S.card\n  let t := s+s/2\n  have hs : 0 < s := by dsimp [s]; omega\n  let D := fun j : Fin 32 => (S.filter (fun i => p j i ∉ S)).map (p j).toEmbedding\n  let U := S ∪ (univ : Finset (Fin 32)).biUnion D\n  have hu : U.card ≤ t := by\n    have hh := card_biUnion_le (s := (univ : Finset (Fin 32))) (t := D)\n    have hh' : ((univ : Finset (Fin 32)).biUnion D).card ≤ _ := hh\n    simp only [D,card_map] at hh'\n    dsimp [U,D,t,s]\n    exact (card_union_le _ _).trans (by omega)\n  obtain ⟨T,hUT,hT⟩ := exists_superset_card_eq hu (by dsimp [t,s]; omega)\n  apply hp\n  apply mem_biUnion.mpr\n  refine ⟨s-1,mem_range.mpr (by omega),?_⟩\n  have eq : s-1+1 = s := by omega\n  simp only [B,eq]\n  refine mem_biUnion.mpr ⟨S,mem_powersetCard.mpr ⟨subset_univ _,rfl⟩,?_⟩\n  refine mem_biUnion.mpr ⟨T,mem_powersetCard.mpr ⟨subset_univ _,hT⟩,?_⟩\n  refine Fintype.mem_piFinset.mpr fun j => mem_filter.mpr ⟨mem_univ _,?_⟩\n  intro x hx\n  obtain ⟨i,hi,rfl⟩ := mem_map.mp hx\n  apply hUT\n  by_cases h : p j i ∈ S\n  · exact mem_union_left _ h\n  · exact mem_union_right _ (mem_biUnion.mpr ⟨j,mem_univ _,mem_map.mpr\n      ⟨i,mem_filter.mpr ⟨hi,h⟩,rfl⟩⟩)\ntheorem expand_bool {A : Type} [Fintype A] [DecidableEq A] :\n    ∃ p : Fin 32 → Equiv.Perm A, ∀ b : A → Bool, ∃ c : Bool,\n      (∑ i, diff (b i) c) ≤ 2 * ∑ j : Fin 32, ∑ i, diff (b i) (b (p j i)) := by\n  obtain ⟨p,hp⟩ := expand_sets (A := A)\n  refine ⟨p,fun b => ?_⟩\n  let S := fun c : Bool => (univ : Finset A).filter (fun i => b i ≠ c)\n  have ha : (S false).card + (S true).card = Fintype.card A := by\n    have h : (univ : Finset A).filter (fun i => ¬ b i ≠ false) = S true := by\n      ext i\n      cases hb : b i <;> simp [S,hb]\n    have he := card_filter_add_card_filter_not (s := (univ : Finset A))\n      (fun i : A => b i ≠ false)\n    rw [h,card_univ] at he\n    exact he\n  have hb (c : Bool) (hc : 2*(S c).card ≤ Fintype.card A) :\n      (∑ i, diff (b i) c) ≤ 2 * ∑ j : Fin 32, ∑ i, diff (b i) (b (p j i)) := by\n    have hh := hp (S c) hc\n    rw [show (∑ i, diff (b i) c) = (S c).card by simp [S,card_filter,diff]]\n    refine hh.trans (Nat.mul_le_mul_left _ (sum_le_sum fun j _ => ?_))\n    rw [filter_filter,card_filter]\n    apply sum_le_sum\n    intro i _\n    by_cases he : b i ≠ c ∧ p j i ∉ S c\n    · have he' : b (p j i) = c := by simpa [S] using he.2\n      simp [he,diff,he',he.1]\n    · simp [he]\n  by_cases h : 2*(S false).card ≤ Fintype.card A\n  · exact ⟨false,hb false h⟩\n  · exact ⟨true,hb true (by omega)⟩\ntheorem expand_fibers {A W : Type} [Fintype A] [Fintype W] (f : A → W) :\n    ∃ p : Fin 32 → Equiv.Perm A, (∀ j i, f (p j i) = f i) ∧\n      ∀ a : A → Bool, ∃ c : W → Bool,\n        (∑ i, diff (a i) (c (f i))) ≤\n          2 * ∑ j : Fin 32, ∑ i, diff (a i) (a (p j i)) := by\n  classical\n  choose p hp using fun w : W => expand_bool (A := {i // f i = w})\n  let E := Equiv.sigmaFiberEquiv f\n  let P : Fin 32 → Equiv.Perm A := fun j =>\n    E.symm.trans ((Equiv.sigmaCongrRight (fun w => p w j)).trans E)\n  have hP (j : Fin 32) (w : W) (i : {i // f i = w}) :\n      P j i = (p w j i : A) := by\n    change P j (E ⟨w,i⟩) = _\n    simp only [P,Equiv.trans_apply,Equiv.symm_apply_apply,Equiv.sigmaCongrRight_apply]\n    rfl\n  refine ⟨P,fun j i => ?_,fun a => ?_⟩\n  · rw [hP j (f i) ⟨i,rfl⟩]\n    exact (p _ _ _).2\n  · choose c hc using fun w : W => hp w (fun i => a i)\n    refine ⟨c,?_⟩\n    have sums (g : A → ℕ) : (∑ i, g i) = ∑ w : W, ∑ i : {i // f i = w}, g i := by\n      calc\n        _ = ∑ i, g (E i) := (Equiv.sum_comp E g).symm\n        _ = _ := Fintype.sum_sigma _\n    rw [sums (fun i => diff (a i) (c (f i)))]\n    simp_rw [sums (fun i => diff (a i) (a (P _ i)))]\n    simp_rw [hP]\n    rw [sum_comm, mul_sum]\n    apply sum_le_sum\n    intro w _\n    simpa only [show ∀ i : {i // f i = w}, f i = w from fun i => i.2] using hc w\nnoncomputable def cycle {A : Type} [Fintype A] : Equiv.Perm A :=\n  (Fintype.equivFin A).trans ((finRotate _).trans (Fintype.equivFin A).symm)\ntheorem cycle_const {A : Type} [Fintype A] (f : A → Bool)\n    (h : ∀ i, f (cycle i) = f i) (i j : A) : f i = f j := by\n  let e := Fintype.equivFin A\n  let g := f ∘ e.symm\n  have hg (k) : g (finRotate _ k) = g k := by simpa [g,e,cycle] using h (e.symm k)\n  have hi : f i = g (e i) := by simp [g]\n  have hj : f j = g (e j) := by simp [g]\n  rw [hi,hj]\n  have finConst (n : ℕ) (g : Fin n → Bool) (hg : ∀ k, g (finRotate n k) = g k)\n      (i j : Fin n) : g i = g j := by\n    cases n with\n    | zero => exact i.elim0\n    | succ n =>\n      have eq (k : Fin (n+1)) : g k = g 0 := by\n        induction k using Fin.induction with\n        | zero => rfl\n        | succ k ih => rw [← ih, ← hg k.castSucc, finRotate_apply, Fin.coeSucc_eq_succ]\n      rw [eq i,eq j]\n  exact finConst _ g hg _ _\nnoncomputable def blockCost {A : Type} [Fintype A] (u : Bool → A → Bool) : ℕ :=\n  (∑ i, diff (u false i) (u true i)) +\n    ∑ i, diff (u false i) (u true (cycle i))\nnoncomputable def dirty {A : Type} (u : Bool → A → Bool) (i : A) : ℕ := by\n  classical\n  exact if ∀ b j, u b j = u false i then 0 else 1\ntheorem dirty_le {A : Type} (u : Bool → A → Bool) (i : A)\n    (b : Bool) (j : A) : diff (u b j) (u false i) ≤ dirty u i := by\n  classical\n  unfold dirty\n  split_ifs with h\n  · simp [h b j,diff_self]\n  · exact diff_le _ _\ntheorem dirty_zero {A : Type} (u : Bool → A → Bool) (i : A)\n    (h : dirty u i = 0) (b : Bool) (j : A) : u b j = u false i :=\n  (diff_zero _ _).mp (Nat.eq_zero_of_le_zero ((dirty_le u i b j).trans (by omega)))\ntheorem dirty_cases {A : Type} (u : Bool → A → Bool) (i : A) :\n    dirty u i = 0 ∨ dirty u i = 1 := by\n  classical\n  unfold dirty\n  split_ifs <;> simp\ntheorem block_bound {A : Type} [Fintype A] (u : Bool → A → Bool) (i : A) :\n    2 * dirty u i ≤ blockCost u := by\n  classical\n  have parity (r : Equiv.Perm A) :\n      (∑ j, diff (u false j) (u true (r j))) +\n        2 * (∑ j, (u false j && u true (r j)).toNat) =\n      (∑ j, (u false j).toNat) + ∑ j, (u true j).toNat := by\n    rw [mul_sum,← sum_add_distrib,← Equiv.sum_comp r (fun j => (u true j).toNat),← sum_add_distrib]\n    refine sum_congr rfl fun j _ => ?_\n    generalize u false j = a\n    generalize u true (r j) = b\n    cases a <;> cases b <;> decide\n  have hzero (h : blockCost u = 0) : dirty u i = 0 := by\n    have h1 (j : A) : u false j = u true j := by\n      apply (diff_zero _ _).mp\n      have hh := single_le_sum (f := fun j => diff (u false j) (u true j))\n        (fun _ _ => Nat.zero_le _) (mem_univ j)\n      unfold blockCost at h\n      omega\n    have h2 (j : A) : u false j = u true (cycle j) := by\n      apply (diff_zero _ _).mp\n      have hh := single_le_sum (f := fun j => diff (u false j) (u true (cycle j)))\n        (fun _ _ => Nat.zero_le _) (mem_univ j)\n      unfold blockCost at h\n      omega\n    have h3 := cycle_const (u true) (fun j => (h2 j).symm.trans (h1 j))\n    have h4 (b : Bool) (j : A) : u b j = u false i := by\n      cases b\n      · exact (h1 j).trans ((h3 j i).trans (h1 i).symm)\n      · exact (h3 j i).trans (h1 i).symm\n    simp [dirty,h4]\n  have p1 := parity (Equiv.refl A)\n  have p2 := parity (cycle (A := A))\n  simp only [Equiv.refl_apply] at p1\n  rcases dirty_cases u i with h | h\n  · simp [h]\n  · have hp : blockCost u ≠ 0 := fun e => by have := hzero e; omega\n    unfold blockCost at *\n    omega\nabbrev TBlock (Q : Type) := (Q × Bool) × Bool\nabbrev KBlock (Q : Type) := (Q × Bool) × Fin 1024\nabbrev CBlock (Q : Type) := KBlock Q × Bool\nabbrev TPos := Option (Fin 1024)\nabbrev CPos := Option (Option (Fin 32))\nabbrev Sites (Q : Type) := (TBlock Q × TPos) ⊕ (CBlock Q × CPos)\nabbrev Assignment (Q : Type) := Bool × Sites Q → Bool\nabbrev Network (Q : Type) := Fin 32 → Equiv.Perm (KBlock Q)\nsection Layout\nvariable {Q W : Type} [Fintype Q]\ndef term (u : Assignment Q) (t : TBlock Q) (b : Bool) (j : TPos) : Bool :=\n  u (b,Sum.inl (t,j))\ndef core (u : Assignment Q) (c : CBlock Q) (b : Bool) (j : CPos) : Bool :=\n  u (b,Sum.inr (c,j))\ndef keyT (t : TBlock Q) : Q := t.1.1\ndef keyK (k : KBlock Q) : Q := k.1.1\ndef atCore (t : TBlock Q) (j : Fin 1024) : CBlock Q := ((t.1,j),!t.2)\ndef linkCoords (Q : Type) : TBlock Q × Fin 1024 ≃ CBlock Q where\n  toFun p := atCore p.1 p.2\n  invFun p := ((p.1.1,!p.2),p.1.2)\n  left_inv := by rintro ⟨⟨q,s⟩,j⟩; cases s <;> rfl\n  right_inv := by rintro ⟨⟨q,j⟩,s⟩; cases s <;> rfl\ndef roleCoords (Q : Type) : (Q × Bool) × Bool ≃ TBlock Q where\n  toFun p := ((p.1.1,p.2),p.1.2)\n  invFun p := ((p.1.1,p.2),p.1.2)\n  left_inv := by rintro ⟨⟨q,r⟩,s⟩; rfl\n  right_inv := by rintro ⟨⟨q,r⟩,s⟩; rfl\ndef sign : Sites Q → Bool\n  | .inl (t,_) => t.2\n  | .inr (c,_) => c.2\ndef owner : Sites Q → Q\n  | .inl (t,_) => keyT t\n  | .inr (c,_) => keyK c.1\ndef canonical (f : Q → W) (w : W → Bool) (p : Bool × Sites Q) : Bool :=\n  xor (w (f (owner p.2))) (sign p.2)\ndef oriented (y : Assignment Q) (p : Bool × Sites Q) : Bool :=\n  xor (y p) (sign p.2)\nnoncomputable def blue : Equiv.Perm (Sites Q) :=\n  (Equiv.prodCongr (Equiv.refl _) cycle).sumCongr (Equiv.prodCongr (Equiv.refl _) cycle)\ndef roleT (x : Q × Bool) (side : Bool) : TBlock Q := ((x.1,side),x.2)\ndef otherSign (x : Q × Bool) : Q × Bool := (x.1,!x.2)\ndef greenFun (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) : Sites Q → Sites Q\n  | .inl (t, none) =>\n    if t.1.2 then .inl (((t.1.1,false),!t.2),none)\n    else .inl (roleT (otherSign (E (t.1.1,t.2))) true,none)\n  | .inl (t,some j) => .inr (atCore t j,none)\n  | .inr (c,none) => .inl (((c.1.1,!c.2),some c.1.2))\n  | .inr (c,some none) => .inr ((c.1,!c.2),some none)\n  | .inr (c,some (some j)) => .inr ((p j c.1,!c.2),some (some j))\ndef greenInv (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) : Sites Q → Sites Q\n  | .inl (t, none) =>\n    if t.1.2 then .inl (roleT (E.symm (t.1.1,!t.2)) false,none)\n    else .inl (((t.1.1,true),!t.2),none)\n  | .inl (t,some j) => .inr (atCore t j,none)\n  | .inr (c,none) => .inl (((c.1.1,!c.2),some c.1.2))\n  | .inr (c,some none) => .inr ((c.1,!c.2),some none)\n  | .inr (c,some (some j)) => .inr (((p j).symm c.1,!c.2),some (some j))\ndef green (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) : Equiv.Perm (Sites Q) where\n  toFun := greenFun E p\n  invFun := greenInv E p\n  left_inv := by\n    rintro (⟨⟨⟨q,r⟩,s⟩,(_|j)⟩ | ⟨⟨⟨⟨q,r⟩,k⟩,s⟩,(_|_|j)⟩)\n    · cases r <;> simp [greenFun,greenInv,roleT,otherSign]\n    · simp [greenFun,greenInv,atCore]\n    · simp [greenFun,greenInv,atCore]\n    · simp [greenFun,greenInv]\n    · simp [greenFun,greenInv]\n  right_inv := by\n    rintro (⟨⟨⟨q,r⟩,s⟩,(_|j)⟩ | ⟨⟨⟨⟨q,r⟩,k⟩,s⟩,(_|_|j)⟩)\n    · cases r <;> simp [greenFun,greenInv,roleT,otherSign]\n    · simp [greenFun,greenInv,atCore]\n    · simp [greenFun,greenInv,atCore]\n    · simp [greenFun,greenInv]\n    · simp [greenFun,greenInv]\ndef greenErr (u : Assignment Q) (a b : Sites Q) : ℕ :=\n  diff (u (false,a)) (xor (u (true,b)) (!(xor (sign a) (sign b))))\ntheorem green_oriented (y : Assignment Q) (a b : Sites Q) :\n    diff (y (false,a)) (!(y (true,b))) = greenErr (oriented y) a b := by\n  unfold greenErr oriented\n  generalize y (false,a) = x\n  generalize y (true,b) = z\n  generalize sign a = r\n  generalize sign b = s\n  cases x <;> cases z <;> cases r <;> cases s <;> decide\ndef extErr (E : Equiv.Perm (Q × Bool))\n    (l r : Q × Bool → Bool) : ℕ :=\n  ∑ x, diff (l x) (xor (r (E x)) (xor x.2 (E x).2))\nnoncomputable def siteCost (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) (y : Assignment Q) : ℕ :=\n  (∑ i, diff (y (false,i)) (y (true,i))) +\n    (∑ i, diff (y (false,i)) (y (true,blue i))) +\n    ∑ i, diff (y (false,i)) (!(y (true,green E p i)))\ndef pn (p : Network Q)\n    (u : Assignment Q) : ℕ :=\n  ∑ j : Fin 32, ∑ k : KBlock Q,\n    diff (core u (k,false) false (some (some j))) (core u (p j k,true) true (some (some j)))\ndef sn (u : Assignment Q) : ℕ :=\n  ∑ k : KBlock Q, diff (core u (k,false) false (some none)) (core u (k,true) true (some none))\ndef ln (u : Assignment Q) : ℕ :=\n  ∑ t : TBlock Q, ∑ j : Fin 1024, diff (term u t false (some j)) (core u (atCore t j) true none)\ndef ext (E : Equiv.Perm (Q × Bool))\n    (u : Assignment Q) : ℕ :=\n  extErr E (fun x => term u (roleT x false) false none)\n    (fun x => term u (roleT (otherSign x) true) true none)\nnoncomputable def sumDirtyT (u : Assignment Q) : ℕ :=\n  ∑ t, dirty (term u t) none\nnoncomputable def sumDirtyC (u : Assignment Q) : ℕ :=\n  ∑ c, dirty (core u c) none\ntheorem diff_chain (a b c d : Bool) : diff a d ≤ diff a b + diff b c + diff c d := by\n  cases a <;> cases b <;> cases c <;> cases d <;> decide\ntheorem green_diff (u : Assignment Q) (a b : Sites Q)\n    (h : sign b = !sign a) : greenErr u a b = diff (u (false,a)) (u (true,b)) := by\n  unfold greenErr\n  rw [h]\n  cases sign a <;> cases u (true,b) <;> rfl\ntheorem green_ext (u : Assignment Q)\n    (E : Equiv.Perm (Q × Bool)) (p : Network Q) (x : Q × Bool) :\n    greenErr u (.inl (roleT x false,none)) (green E p (.inl (roleT x false,none))) =\n      diff (term u (roleT x false) false none)\n        (xor (term u (roleT (otherSign (E x)) true) true none) (xor x.2 (E x).2)) := by\n  change diff _ (xor _ (!(xor x.2 (!(E x).2)))) = diff _ (xor _ (xor x.2 (E x).2))\n  have h : ∀ a b : Bool, (!(xor a (!b))) = xor a b := by decide\n  rw [h]\n  rfl\ntheorem split_cost (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) (y : Assignment Q) :\n    let u := oriented y\n    siteCost E p y =\n      (∑ t : TBlock Q, blockCost (term u t)) + (∑ c : CBlock Q, blockCost (core u c)) +\n      ext E u + ln u +\n      (∑ x : Q × Bool, diff (term u (roleT x true) false none)\n        (term u (roleT (otherSign x) false) true none)) +\n      (∑ c : CBlock Q, diff (core u c false none)\n        (term u (c.1.1,!c.2) true (some c.1.2))) +\n      (∑ c : CBlock Q, diff (core u c false (some none)) (core u (c.1,!c.2) true (some none))) +\n      ∑ j : Fin 32, ∑ c : CBlock Q,\n        diff (core u c false (some (some j))) (core u (p j c.1,!c.2) true (some (some j))) := by\n  intro u\n  have r (i : Sites Q) : diff (y (false,i)) (y (true,i)) = diff (u (false,i)) (u (true,i)) :=\n    (diff_xor _ _ _).symm\n  have b (i : Sites Q) : diff (y (false,i)) (y (true,blue i)) =\n      diff (u (false,i)) (u (true,blue i)) := by\n    have h : sign (blue i) = sign i := by cases i <;> rfl\n    exact (by simpa only [u,oriented,h] using (diff_xor (y (false,i)) (y (true,blue i)) (sign i)).symm)\n  have g (i : Sites Q) : diff (y (false,i)) (!(y (true,green E p i))) =\n      greenErr u i (green E p i) := green_oriented _ _ _\n  have tr (x : Q × Bool) :\n      greenErr u (.inl (roleT x true,none)) (green E p (.inl (roleT x true,none))) =\n      diff (term u (roleT x true) false none) (term u (roleT (otherSign x) false) true none) :=\n    green_diff u _ _ rfl\n  have tl (a : TBlock Q) (j : Fin 1024) :\n      greenErr u (.inl (a,some j)) (green E p (.inl (a,some j))) =\n      diff (term u a false (some j)) (core u (atCore a j) true none) :=\n    green_diff u _ _ rfl\n  have cl (c : CBlock Q) :\n      greenErr u (.inr (c,none)) (green E p (.inr (c,none))) =\n      diff (core u c false none) (term u (c.1.1,!c.2) true (some c.1.2)) :=\n    green_diff u _ _ rfl\n  have cs (c : CBlock Q) :\n      greenErr u (.inr (c,some none)) (green E p (.inr (c,some none))) =\n      diff (core u c false (some none)) (core u (c.1,!c.2) true (some none)) :=\n    green_diff u _ _ rfl\n  have cp (c : CBlock Q) (j : Fin 32) :\n      greenErr u (.inr (c,some (some j))) (green E p (.inr (c,some (some j)))) =\n      diff (core u c false (some (some j))) (core u (p j c.1,!c.2) true (some (some j))) :=\n    green_diff u _ _ rfl\n  have bT (t : TBlock Q) (j : TPos) : blue (.inl (t,j)) = .inl (t,cycle j) := rfl\n  have bC (c : CBlock Q) (j : CPos) : blue (.inr (c,j)) = .inr (c,cycle j) := rfl\n  have tEq (t : TBlock Q) b j : u (b,Sum.inl (t,j)) = term u t b j := rfl\n  have cEq (c : CBlock Q) b j : u (b,Sum.inr (c,j)) = core u c b j := rfl\n  have h0 (q : Q) (s : Bool) :\n      greenErr u (.inl (((q,false),s),none)) (green E p (.inl (((q,false),s),none))) = _ :=\n    green_ext u E p (q,s)\n  have h1 (q : Q) (s : Bool) :\n      greenErr u (.inl (((q,true),s),none)) (green E p (.inl (((q,true),s),none))) = _ :=\n    tr (q,s)\n  unfold siteCost\n  simp_rw [r,b,g]\n  simp only [Fintype.sum_sum_type]\n  rw [sum_comm (f := fun j c => diff (core u c false (some (some j)))\n    (core u (p j c.1,!c.2) true (some (some j))))]\n  simp only [Fintype.sum_prod_type,Fintype.sum_option,Fintype.sum_bool,bT,bC,tEq,cEq,\n    blockCost,ext,extErr,ln,tl,cl,cs,cp,h0,h1,roleT,otherSign,Prod.fst,Prod.snd]\n  simp only [sum_add_distrib]\n  ac_rfl\ntheorem site_lower (E : Equiv.Perm (Q × Bool))\n    (p : Network Q) (y : Assignment Q) :\n    let u := oriented y\n    2*sumDirtyT u + 2*sumDirtyC u + ext E u + ln u + sn u + pn p u ≤ siteCost E p y := by\n  intro u\n  rw [split_cost]\n  have t : 2*sumDirtyT u ≤ ∑ t : TBlock Q, blockCost (term u t) := by\n    simp only [sumDirtyT,mul_sum]\n    exact sum_le_sum fun t _ => block_bound (term u t) none\n  have c : 2*sumDirtyC u ≤ ∑ c : CBlock Q, blockCost (core u c) := by\n    simp only [sumDirtyC,mul_sum]\n    exact sum_le_sum fun c _ => block_bound (core u c) none\n  have s : sn u ≤ ∑ c : CBlock Q,\n      diff (core u c false (some none)) (core u (c.1,!c.2) true (some none)) := by\n    unfold sn\n    rw [Fintype.sum_prod_type (f := fun c : CBlock Q => _)]\n    exact sum_le_sum fun k _ => by rw [Fintype.sum_bool]; simp only [Prod.fst,Prod.snd,Bool.not_true,Bool.not_false]; omega\n  have h : pn p u ≤ ∑ j : Fin 32, ∑ c : CBlock Q,\n      diff (core u c false (some (some j))) (core u (p j c.1,!c.2) true (some (some j))) := by\n    unfold pn\n    apply sum_le_sum fun j _ => ?_\n    rw [Fintype.sum_prod_type (f := fun c : CBlock Q => _)]\n    exact sum_le_sum fun k _ => by rw [Fintype.sum_bool]; simp only [Prod.fst,Prod.snd,Bool.not_true,Bool.not_false]; omega\n  dsimp [u] at *\n  omega\ndef coreRep (u : Assignment Q) (c : CBlock Q) :=\n  core u c false none\ndef termRep (u : Assignment Q) (t : TBlock Q) :=\n  term u t false none\ntheorem core_dist (f : Q → W)\n    (p : Network Q)\n    (hp : ∀ b : KBlock Q → Bool, ∃ w : W → Bool,\n      (∑ k, diff (b k) (w (f (keyK k)))) ≤\n        2 * ∑ j : Fin 32, ∑ k, diff (b k) (b (p j k)))\n    (u : Assignment Q) :\n    ∃ w : W → Bool, (∑ c : CBlock Q, diff (coreRep u c) (w (f (keyK c.1)))) ≤\n      257*sumDirtyC u + 129*sn u + 4*pn p u := by\n  let a := coreRep u\n  let d := fun c : CBlock Q => dirty (core u c) none\n  have hpair (r : Equiv.Perm (KBlock Q)) :\n      (∑ k : KBlock Q, (d (k,false) + d (r k,true))) = sumDirtyC u := by\n    rw [sum_add_distrib, Equiv.sum_comp r (fun k => d (k,true))]\n    unfold sumDirtyC\n    rw [Fintype.sum_prod_type (f := fun c : CBlock Q => d c)]\n    simp only [Fintype.sum_bool,sum_add_distrib]\n    exact add_comm _ _\n  have hs : (∑ k : KBlock Q, diff (a (k,false)) (a (k,true))) ≤ sumDirtyC u + sn u := by\n    rw [← hpair (Equiv.refl _),sn,← sum_add_distrib]\n    refine sum_le_sum fun k _ => ?_\n    have h := diff_chain (a (k,false)) (core u (k,false) false (some none))\n      (core u (k,true) true (some none)) (a (k,true))\n    have hl := dirty_le (core u (k,false)) none false (some none)\n    have hr := dirty_le (core u (k,true)) none true (some none)\n    rw [diff_symm (a (k,false)) (core u (k,false) false (some none))] at h\n    change diff (core u (k,false) false (some none)) (a (k,false)) ≤ d (k,false) at hl\n    change diff (core u (k,true) true (some none)) (a (k,true)) ≤ d (k,true) at hr\n    change diff (a (k,false)) (a (k,true)) ≤ d (k,false) + d (k,true) + _\n    omega\n  have hj (j : Fin 32) :\n      (∑ k : KBlock Q, diff (a (k,false)) (a (p j k,false))) ≤\n      (sumDirtyC u + (∑ k : KBlock Q, diff (a (k,false)) (a (k,true)))) +\n      ∑ k : KBlock Q, diff (core u (k,false) false (some (some j))) (core u (p j k,true) true (some (some j))) := by\n    rw [← hpair (p j), ← Equiv.sum_comp (p j) (fun k => diff (a (k,false)) (a (k,true)))]\n    simp_rw [← sum_add_distrib]\n    refine sum_le_sum fun k _ => ?_\n    let l := core u (k,false) false (some (some j))\n    let r := core u (p j k,true) true (some (some j))\n    have h := diff_chain (a (k,false)) l r (a (p j k,true))\n    have hh := diff_tri (a (k,false)) (a (p j k,true)) (a (p j k,false))\n    have hl : diff (a (k,false)) l ≤ d (k,false) := by\n      rw [diff_symm (a (k,false)) l]\n      exact dirty_le (core u (k,false)) none false (some (some j))\n    have hr : diff r (a (p j k,true)) ≤ d (p j k,true) :=\n      dirty_le (core u (p j k,true)) none true (some (some j))\n    rw [diff_symm (a (p j k,true)) (a (p j k,false))] at hh\n    change diff (a (k,false)) (a (p j k,false)) ≤ _ + diff l r\n    omega\n  have hcut : (∑ j : Fin 32, ∑ k : KBlock Q, diff (a (k,false)) (a (p j k,false))) ≤\n      64*sumDirtyC u + 32*sn u + pn p u := by\n    have h := sum_le_sum (s := (univ : Finset (Fin 32))) (fun j _ => hj j)\n    rw [sum_add_distrib,sum_const,card_univ,Fintype.card_fin,smul_eq_mul,← pn] at h\n    omega\n  obtain ⟨w,hw⟩ := hp (fun k => a (k,false))\n  refine ⟨w,?_⟩\n  have hsum : (∑ k : KBlock Q, diff (a (k,true)) (w (f (keyK k)))) ≤\n      (∑ k : KBlock Q, diff (a (k,false)) (a (k,true))) +\n      ∑ k : KBlock Q, diff (a (k,false)) (w (f (keyK k))) := by\n    rw [← sum_add_distrib]\n    exact sum_le_sum fun k _ => by\n      rw [diff_symm (a (k,false)) (a (k,true))]\n      exact diff_tri _ _ _\n  rw [Fintype.sum_prod_type]\n  simp_rw [Fintype.sum_bool,sum_add_distrib]\n  change (∑ k, diff (a (k,true)) _) + (∑ k, diff (a (k,false)) _) ≤ _\n  omega\nnoncomputable def cleanErr (f : Q → W) (w : W → Bool)\n    (u : Assignment Q) (t : TBlock Q) :=\n  if dirty (term u t) none = 0 then diff (termRep u t) (w (f (keyT t))) else 0\ntheorem term_bound (f : Q → W) (w : W → Bool)\n    (u : Assignment Q) (t : TBlock Q) (b : Bool) (j : TPos) :\n    diff (term u t b j) (w (f (keyT t))) ≤ dirty (term u t) none + cleanErr f w u t := by\n  rcases dirty_cases (term u t) none with h | h\n  · rw [dirty_zero _ _ h]\n    simp [h,cleanErr,termRep]\n  · simpa [h,cleanErr] using diff_le (term u t b j) (w (f (keyT t)))\ntheorem clean_bound (f : Q → W) (w : W → Bool)\n    (u : Assignment Q) :\n    1024*(∑ t, cleanErr f w u t) ≤ ln u + sumDirtyC u +\n      ∑ c : CBlock Q, diff (coreRep u c) (w (f (keyK c.1))) := by\n  let g := fun c : CBlock Q => dirty (core u c) none + diff (coreRep u c) (w (f (keyK c.1)))\n  let le := fun t j => diff (term u t false (some j)) (core u (atCore t j) true none)\n  have h (t : TBlock Q) (j : Fin 1024) :\n      cleanErr f w u t ≤ le t j + g (atCore t j) := by\n    unfold cleanErr\n    split_ifs with h\n    · dsimp [le,g]\n      rw [dirty_zero _ _ h false (some j)]\n      have h0 := diff_chain (termRep u t) (core u (atCore t j) true none)\n        (coreRep u (atCore t j)) (w (f (keyT t)))\n      have h1 := dirty_le (core u (atCore t j)) none true none\n      change diff _ (coreRep u (atCore t j)) ≤ _ at h1\n      change diff (termRep u t) _ ≤ diff (termRep u t) _ + (_ + diff _ (w (f (keyT t))))\n      omega\n    · exact Nat.zero_le _\n  have hh := sum_le_sum (s := (univ : Finset (TBlock Q))) (fun t _ =>\n    sum_le_sum (s := (univ : Finset (Fin 1024))) (fun j _ => h t j))\n  simp only [sum_const,card_univ,Fintype.card_fin,smul_eq_mul,← mul_sum] at hh\n  have eq : (∑ t : TBlock Q, ∑ j : Fin 1024, g (atCore t j)) = ∑ c, g c := by\n    calc\n      _ = ∑ x : TBlock Q × Fin 1024, g (linkCoords Q x) := (Fintype.sum_prod_type _).symm\n      _ = _ := Equiv.sum_comp (linkCoords Q) g\n  simp only [sum_add_distrib] at hh\n  rw [eq] at hh\n  simpa [le,g,ln,sum_add_distrib,sumDirtyC,add_assoc] using hh\ndef target (E : Equiv.Perm (Q × Bool))\n    (f : Q → W) (w : W → Bool) : ℕ :=\n  extErr E (fun x => w (f x.1)) (fun x => w (f x.1))\ntheorem ext_bound (E : Equiv.Perm (Q × Bool))\n    (f : Q → W) (w : W → Bool) (u : Assignment Q) :\n    target E f w ≤ ext E u + sumDirtyT u + ∑ t, cleanErr f w u t := by\n  let m := fun t : TBlock Q => dirty (term u t) none + cleanErr f w u t\n  let R := (Equiv.prodCongr (Equiv.refl Q) (Function.Involutive.toPerm Bool.not Bool.not_not)).trans E.symm\n  have h (x : Q × Bool) :\n      diff (w (f x.1)) (xor (w (f (E x).1)) (xor x.2 (E x).2)) ≤\n      diff (term u (roleT x false) false none)\n        (xor (term u (roleT (otherSign (E x)) true) true none) (xor x.2 (E x).2)) +\n        (m (roleT x false) + m (roleT (otherSign (E x)) true)) := by\n    have hh := diff_chain (w (f x.1)) (term u (roleT x false) false none)\n      (xor (term u (roleT (otherSign (E x)) true) true none) (xor x.2 (E x).2))\n      (xor (w (f (E x).1)) (xor x.2 (E x).2))\n    rw [diff_xor,diff_symm (w (f x.1)) (term u (roleT x false) false none)] at hh\n    have hl := term_bound f w u (roleT x false) false none\n    have hr := term_bound f w u (roleT (otherSign (E x)) true) true none\n    change diff _ (w (f x.1)) ≤ m (roleT x false) at hl\n    change diff _ (w (f (E x).1)) ≤ m (roleT (otherSign (E x)) true) at hr\n    omega\n  have hh := sum_le_sum (s := (univ : Finset (Q × Bool))) (fun x _ => h x)\n  simp only [sum_add_distrib] at hh\n  have he := Equiv.sum_comp (R.symm) (fun x => m (roleT x true))\n  have er (x : Q × Bool) : R.symm x = otherSign (E x) := rfl\n  simp only [er] at he\n  rw [he] at hh\n  have hm := Equiv.sum_comp (roleCoords Q) m\n  rw [Fintype.sum_prod_type (f := fun p : (Q × Bool) × Bool => m (roleCoords Q p))] at hm\n  simp only [Fintype.sum_bool,sum_add_distrib] at hm\n  change (∑ x : Q × Bool, m (roleT x true)) + (∑ x : Q × Bool, m (roleT x false)) = _ at hm\n  change target E f w ≤ ext E u + (_ + _) at hh\n  rw [add_comm (∑ x : Q × Bool, m (roleT x false)),hm] at hh\n  simpa [m,sum_add_distrib,sumDirtyT,add_assoc] using hh\ndef distance {A : Type} [Fintype A] (x y : A → Bool) : ℕ :=\n  ∑ i, diff (x i) (y i)\ntheorem layout_dist (f : Q → W) (w : W → Bool)\n    (y : Assignment Q) :\n    let u := oriented y\n    distance y (canonical f w) ≤\n      2050*(sumDirtyT u + ∑ t, cleanErr f w u t) +\n      68*(sumDirtyC u + ∑ c : CBlock Q, diff (coreRep u c) (w (f (keyK c.1)))) := by\n  intro u\n  have e (b : Bool) (i : Sites Q) :\n      diff (y (b,i)) (canonical f w (b,i)) = diff (u (b,i)) (w (f (owner i))) := by\n    unfold canonical\n    rw [← diff_xor _ _ (sign i),xor_cancel]\n    rfl\n  have hT (b : Bool) (t : TBlock Q) :\n      (∑ j : TPos, diff (term u t b j) (w (f (keyT t)))) ≤\n      1025*(dirty (term u t) none + cleanErr f w u t) := by\n    simpa only [sum_const,card_univ,Fintype.card_option,Fintype.card_fin,smul_eq_mul] using\n      sum_le_sum (s := (univ : Finset TPos)) (fun j _ => term_bound f w u t b j)\n  have hC (b : Bool) (c : CBlock Q) :\n      (∑ j : CPos, diff (core u c b j) (w (f (keyK c.1)))) ≤\n      34*(dirty (core u c) none + diff (coreRep u c) (w (f (keyK c.1)))) := by\n    have h (j : CPos) := (diff_tri (core u c b j) (coreRep u c) (w (f (keyK c.1)))).trans\n      (Nat.add_le_add_right (dirty_le (core u c) none b j) _)\n    simpa only [sum_const,card_univ,Fintype.card_option,Fintype.card_fin,smul_eq_mul] using\n      sum_le_sum (s := (univ : Finset CPos)) (fun j _ => h j)\n  have h (b : Bool) :\n      (∑ i : Sites Q, diff (u (b,i)) (w (f (owner i)))) ≤\n      1025*(sumDirtyT u + ∑ t, cleanErr f w u t) +\n      34*(sumDirtyC u + ∑ c : CBlock Q, diff (coreRep u c) (w (f (keyK c.1)))) := by\n    simp only [Fintype.sum_sum_type]\n    rw [Fintype.sum_prod_type (f := fun p : TBlock Q × TPos => _),\n      Fintype.sum_prod_type (f := fun p : CBlock Q × CPos => _)]\n    have ht := sum_le_sum (s := (univ : Finset (TBlock Q))) (fun t _ => hT b t)\n    have hc := sum_le_sum (s := (univ : Finset (CBlock Q))) (fun c _ => hC b c)\n    simp only [sumDirtyT,sumDirtyC,mul_sum,← sum_add_distrib]\n    exact Nat.add_le_add ht hc\n  rw [distance,Fintype.sum_prod_type]\n  simp only [e,Fintype.sum_bool]\n  have := h true\n  have := h false\n  omega\ntheorem layout_round [Fintype W]\n    (E : Equiv.Perm (Q × Bool)) (f : Q → W) :\n    ∃ p : Network Q, (∀ j k, f (keyK (p j k)) = f (keyK k)) ∧\n      ∀ y : Assignment Q, ∃ w : W → Bool,\n        let u := oriented y\n        let I := sumDirtyT u + sumDirtyC u + ln u + sn u + pn p u\n        distance y (canonical f w) ≤ 40000*I ∧ 2*target E f w + I ≤ 2*siteCost E p y := by\n  obtain ⟨p,hp,h⟩ := expand_fibers (fun k : KBlock Q => f (keyK k))\n  refine ⟨p,hp,fun y => ?_⟩\n  let u := oriented y\n  obtain ⟨w,hw⟩ := core_dist f p h u\n  refine ⟨w,?_⟩\n  dsimp only\n  have hcl := clean_bound f w u\n  have hex := ext_bound E f w u\n  have hd := layout_dist f w y\n  have hl := site_lower E p y\n  dsimp only at hd hl\n  dsimp [u] at hw hcl hex\n  constructor <;> omega\ntheorem oriented_canonical (f : Q → W) (w : W → Bool)\n    (b : Bool) (i : Sites Q) : oriented (canonical f w) (b,i) = w (f (owner i)) :=\n  xor_cancel _ _\ntheorem layout_exact (E : Equiv.Perm (Q × Bool))\n    (f : Q → W) (p : Network Q)\n    (hp : ∀ j k, f (keyK (p j k)) = f (keyK k)) (w : W → Bool) :\n    siteCost E p (canonical f w) = target E f w := by\n  rw [split_cost]\n  simp only [term,core,blockCost,ln,ext,target,extErr,oriented_canonical,owner,keyT,keyK,atCore,\n    roleT,otherSign,Prod.fst,Prod.snd,diff_self,sum_const_zero,zero_add,add_zero]\n  have h (j : Fin 32) (k : KBlock Q) : f ((p j k).1.1) = f k.1.1 := hp j k\n  simp only [h,diff_self,sum_const_zero,add_zero]\ndef flp : Sites Q → Sites Q\n  | .inl (t,j) => .inl ((t.1,!t.2),j)\n  | .inr (c,j) => .inr ((c.1,!c.2),j)\ntheorem canon_flip (f : Q → W) (w : W → Bool) (b : Bool) (s : Sites Q) :\n    canonical f w (b,flp s) = !canonical f w (b,s) := by\n  rcases s with ⟨⟨t,c⟩,j⟩ | ⟨⟨k,c⟩,j⟩ <;> simp [canonical,flp,owner,sign,keyT,keyK]\ntheorem canon_card (f : Q → W) (w : W → Bool) :\n    (univ.filter (fun p : Bool × Sites Q => canonical f w p = true)).card = Fintype.card (Sites Q) := by\n  let F := fun s : Sites Q => if canonical f w (false,s) = true then 1 else 0\n  have hf : ∑ s, F (flp s) = ∑ s, F s := Equiv.sum_comp (Function.Involutive.toPerm flp\n    (by rintro (⟨⟨t,c⟩,j⟩|⟨⟨k,c⟩,j⟩) <;> simp [flp])) F\n  have hs : ∑ s, (F s + F (flp s)) = Fintype.card (Sites Q) := by\n    rw [Fintype.card_eq_sum_ones]\n    refine sum_congr rfl fun s _ => ?_\n    simp only [F,canon_flip]\n    cases canonical f w (false,s) <;> rfl\n  rw [sum_add_distrib,hf] at hs\n  rw [card_filter,Fintype.sum_prod_type,Fintype.sum_bool]\n  change ∑ s, F s + ∑ s, F s = _\n  omega\ntheorem sum_owner (G : Q → ℕ) : ∑ s : Sites Q, G (owner s) = 143364 * ∑ q, G q := by\n  simp only [Fintype.sum_sum_type,Fintype.sum_prod_type,owner,keyT,keyK,sum_const,card_univ,\n    Fintype.card_option,Fintype.card_fin,smul_eq_mul,Fintype.card_bool]\n  rw [mul_sum,← sum_add_distrib]\n  refine sum_congr rfl fun q _ => ?_\n  ring\ntheorem canon_dist (f : Q → W) (w w' : W → Bool) :\n    distance (canonical f w) (canonical f w') = 286728 * ∑ q, diff (w (f q)) (w' (f q)) := by\n  have h := sum_owner (fun q => diff (w (f q)) (w' (f q)))\n  simp only [distance,canonical,diff_xor,Fintype.sum_prod_type,Fintype.sum_bool]\n  omega\ntheorem dist_tri {A : Type} [Fintype A] (x y z : A → Bool) :\n    distance x z ≤ distance x y + distance y z := by\n  unfold distance\n  rw [← sum_add_distrib]\n  exact sum_le_sum fun i _ => diff_tri _ _ _\ntheorem card_tr {A B : Type} [Fintype A] [Fintype B] (e : A ≃ B) (P : B → Prop) [DecidablePred P] :\n    (univ.filter P).card = (univ.filter (fun a => P (e a))).card :=\n  card_equiv e.symm (by simp)\ntheorem dist_tr {n : ℕ} (e : Sites Q ≃ Fin n) (y z : Bool × Fin n → Bool) :\n    (univ.filter (fun q => y q ≠ z q)).card =\n      distance (fun q : Bool × Sites Q => y (q.1,e q.2)) (fun q => z (q.1,e q.2)) := by\n  rw [card_tr (Equiv.prodCongr (Equiv.refl Bool) e),card_filter]\n  rfl\ntheorem cost_tr {n : ℕ} (e : Sites Q ≃ Fin n) (E : Equiv.Perm (Q × Bool)) (p : Network Q)\n    (y : Bool × Fin n → Bool) :\n    (univ.filter (fun i : Fin n => y (false,i) ≠ y (true,i))).card +\n    (univ.filter (fun i : Fin n => y (false,i) ≠ y (true,(e.symm.trans (blue.trans e)) i))).card +\n    (univ.filter (fun i : Fin n => y (false,i) = y (true,(e.symm.trans ((green E p).trans e)) i))).card =\n    siteCost E p (fun q => y (q.1,e q.2)) := by\n  rw [card_tr e,card_tr e,card_tr e]\n  simp only [siteCost,card_filter,Equiv.trans_apply,Equiv.symm_apply_apply]\n  congr 1\n  refine sum_congr rfl fun s _ => ?_\n  cases y (false,e s) <;> cases y (true,e (green E p s)) <;> rfl\nend Layout\ndef gc (a b z h e : Bool) : ℕ := triangle a b h + triangle a h e + triangle b h e + diff z h\ntheorem gc_exact (a b z r : Bool) :\n    gc (xor a r) (xor b r) (xor z r) (xor (!(a && b)) r) r = 3 + diff z (!(a && b)) := by\n  cases a <;> cases b <;> cases z <;> cases r <;> decide\ntheorem gc_repair (a b z h r : Bool) :\n    3 + diff z (!(a && b)) + diff h (!(a && b)) ≤ gc (xor a r) (xor b r) (xor z r) (xor h r) r := by\n  cases a <;> cases b <;> cases z <;> cases h <;> cases r <;> decide\ndef nx : Fin 11 → Fin 11 := ![1,2,0,4,5,3,7,8,6,10,9]\ndef pv : Fin 11 → Fin 11 := ![2,0,1,5,3,4,8,6,7,10,9]\ndef pf (m : Fin 11 → Fin 11) (p : Fin 11 × Bool) : Fin 11 × Bool :=\n  if p.1.val < 9 then (m p.1,!p.2) else if p.2 then p else (m p.1,false)\ndef piE : Equiv.Perm (Fin 11 × Bool) :=\n  ⟨pf nx,pf pv,fun p => by revert p; decide,fun p => by revert p; decide⟩\ntheorem gate_sum (A B Z H R : Bool) : ∑ k : Fin 11, ∑ s : Bool,\n    diff (![A,B,H,A,H,R,B,H,R,Z,H] k)\n      (xor (![A,B,H,A,H,R,B,H,R,Z,H] (piE (k,s)).1) (xor s (piE (k,s)).2)) = 2 * gc A B Z H R := by\n  revert A B Z H R\n  decide\nabbrev Wq (v g : ℕ) := Fin (v+1) ⊕ Fin g × Fin 11\nabbrev Ww (v g : ℕ) := Fin (v+1) ⊕ Fin g\ndef fw {v g : ℕ} (a b z : Fin g → Fin v) : Wq v g → Ww v g\n  | .inl i => .inl i\n  | .inr (j,k) => ![.inl (a j).succ,.inl (b j).succ,.inr j,.inl (a j).succ,.inr j,.inl 0,.inl (b j).succ,\n      .inr j,.inl 0,.inl (z j).succ,.inr j] k\ndef eF {v g : ℕ} (π : Equiv.Perm (Fin 11 × Bool)) : Wq v g × Bool → Wq v g × Bool\n  | (.inl i,s) => (.inl i,s)\n  | (.inr (j,k),s) => (.inr (j,(π (k,s)).1),(π (k,s)).2)\ndef Ew (v g : ℕ) : Equiv.Perm (Wq v g × Bool) where\n  toFun := eF piE\n  invFun := eF piE.symm\n  left_inv := by rintro ⟨(i|⟨j,k⟩),s⟩ <;> simp [eF]\n  right_inv := by rintro ⟨(i|⟨j,k⟩),s⟩ <;> simp [eF]\ndef wenc {v g : ℕ} (a b : Fin g → Fin v) (x : Fin v → Bool) (r : Bool) : Ww v g → Bool\n  | .inl i => xor (Matrix.vecCons false x i) r\n  | .inr j => xor (!(x (a j) && x (b j))) r\ntheorem target_eq {v g : ℕ} (a b z : Fin g → Fin v) (w : Ww v g → Bool) :\n    target (Ew v g) (fw a b z) w = ∑ j, 2 * gc (w (.inl (a j).succ)) (w (.inl (b j).succ))\n      (w (.inl (z j).succ)) (w (.inr j)) (w (.inl 0)) := by\n  unfold target extErr\n  rw [Fintype.sum_prod_type,Fintype.sum_sum_type,Fintype.sum_prod_type,sum_eq_zero,zero_add]\n  · refine sum_congr rfl fun j _ => ?_\n    rw [← gate_sum]\n    refine sum_congr rfl fun k _ => sum_congr rfl fun s _ => ?_\n    fin_cases k <;> cases s <;> rfl\n  · intro i _\n    simp [Ew,eF,fw,diff_self]\ntheorem rep_lemma {v g : ℕ} (a b z : Fin g → Fin v) (w : Ww v g → Bool) (x : Fin v → Bool)\n    (r : Bool) (hr : w (.inl 0) = r) (hx : ∀ i, w (.inl i.succ) = xor (x i) r) :\n    6*g + 2*(∑ j, diff (x (z j)) (!(x (a j) && x (b j)))) +\n      2*(∑ j, diff (xor (w (.inr j)) r) (!(x (a j) && x (b j)))) ≤ target (Ew v g) (fw a b z) w ∧\n    ∑ q, diff (w (fw a b z q)) (wenc a b x r (fw a b z q)) ≤\n      11*∑ j, diff (xor (w (.inr j)) r) (!(x (a j) && x (b j))) := by\n  have hv : ∀ i, wenc a b x r (.inl i) = w (.inl i) := by\n    intro i\n    induction i using Fin.cases with\n    | zero => simp [wenc,hr]\n    | succ i => simp [wenc,hx]\n  constructor\n  · rw [target_eq]\n    have h := fun j => gc_repair (x (a j)) (x (b j)) (x (z j)) (xor (w (.inr j)) r) r\n    simp only [xor_cancel] at h\n    have := sum_le_sum fun j (_ : j ∈ univ) => Nat.mul_le_mul_left 2 (h j)\n    simp only [mul_add,sum_add_distrib,sum_const,card_univ,Fintype.card_fin,smul_eq_mul,← mul_sum] at this\n    simp only [hx,hr,← mul_sum]\n    omega\n  · rw [Fintype.sum_sum_type,Fintype.sum_prod_type,sum_eq_zero fun i _ => by simp [fw,hv,diff_self],\n      zero_add,mul_sum]\n    refine sum_le_sum fun j _ => ?_\n    have hj : diff (w (.inr j)) (wenc a b x r (.inr j)) =\n        diff (xor (w (.inr j)) r) (!(x (a j) && x (b j))) := by\n      rw [← diff_xor _ _ r]\n      simp [wenc,xor_cancel]\n    rw [← hj]\n    have := sum_le_sum fun k (_ : k ∈ univ) => (show diff (w (fw a b z (.inr (j,k))))\n      (wenc a b x r (fw a b z (.inr (j,k)))) ≤ diff (w (.inr j)) (wenc a b x r (.inr j)) by\n        fin_cases k <;> simp [fw,hv,diff_self])\n    simpa using this\ntheorem card_le (v g : ℕ) : Fintype.card (Sites (Wq v g)) ≤ 2000000*(v+g+1) := by\n  simp only [Fintype.card_sum,Fintype.card_prod,Fintype.card_option,Fintype.card_fin,Fintype.card_bool]\n  omega\ntheorem _root_.PEqualNP.Outside.L_pcx_409 : ∃ C : ℕ, 0 < C ∧ ∀ (v g : ℕ) (a b z : Fin g → Fin v),\n  let X := Fin v → Bool\n  let add := fun (s t : Bool) => decide (s ≠ t)\n  let bad := fun (x : X) => (Finset.univ.filter (fun j : Fin g =>\n    x (z j) ≠ !(x (a j) && x (b j)))).card\n  ∃ n : ℕ, n ≤ C*(v+g+1) ∧\n    let V := Bool × Fin n\n    let Y := V → Bool\n    ∃ (σ τ : Equiv.Perm (Fin n)) (mark : (Fin (v+1) × Bool) ↪ V)\n        (enc : X → Bool → Y) (dec : Y → X × Bool),\n      let cost := fun (y : Y) =>\n        (Finset.univ.filter (fun i : Fin n => y (false,i) ≠ y (true,i))).card +\n        (Finset.univ.filter (fun i : Fin n => y (false,i) ≠ y (true,σ i))).card +\n        (Finset.univ.filter (fun i : Fin n => y (false,i) = y (true,τ i))).card\n      (∀ q, (mark q).1 = false) ∧\n      (∀ (x : X) (r : Bool), dec (enc x r) = (x,r) ∧\n        (∀ s : Bool, enc x r (mark (0,s)) = add r s) ∧\n        (∀ (i : Fin v) (s : Bool), enc x r (mark (i.succ,s)) = add (add (x i) r) s) ∧\n        (Finset.univ.filter (fun p : V => enc x r p = true)).card = n ∧\n        cost (enc x r) = 6*g + 2*bad x) ∧\n      (∀ y : Y, (Finset.univ.filter (fun p : V =>\n        y p ≠ enc (dec y).1 (dec y).2 p)).card + C*(6*g + 2*bad (dec y).1) ≤ C*cost y) ∧\n      ∀ pins : Fin v → Option Bool,\n        (Finset.univ.filter (fun y : Y => cost y = 6*g ∧ y (mark (0,false)) = false ∧\n          ∀ (i : Fin v) (s : Bool), pins i = some s → y (mark (i.succ,s)) = false)).card =\n        (Finset.univ.filter (fun x : X => bad x = 0 ∧\n          ∀ (i : Fin v) (s : Bool), pins i = some s → x i = s)).card := by\n  classical\n  refine ⟨2000000,by norm_num,fun v g a b z => ?_⟩\n  intro X add bad\n  obtain ⟨p,hp,hr⟩ := layout_round (Ew v g) (fw a b z)\n  choose W hW using hr\n  let e := Fintype.equivFin (Sites (Wq v g))\n  refine ⟨_,card_le v g,?_⟩\n  intro V Y\n  have hadd : ∀ r s : Bool, add r s = xor r s := fun r s => by cases r <;> cases s <;> rfl\n  obtain ⟨mark,hmk⟩ : ∃ mark : (Fin (v+1) × Bool) ↪ V,\n      ∀ q, mark q = (false,e (.inl ((((.inl q.1 : Wq v g),false),q.2),none))) :=\n    ⟨⟨fun q => (false,e (.inl ((((.inl q.1 : Wq v g),false),q.2),none))),\n      fun ⟨i,s⟩ ⟨i',s'⟩ h => by\n        have h2 := e.injective (congrArg Prod.snd h)\n        simp at h2\n        simpa using h2⟩,fun _ => rfl⟩\n  obtain ⟨enc,henc⟩ : ∃ enc : X → Bool → Y, ∀ x r q,\n      enc x r q = canonical (fw a b z) (wenc a b x r) (q.1,e.symm q.2) := ⟨_,fun _ _ _ => rfl⟩\n  have hE : ∀ x r, (fun q : Bool × Sites (Wq v g) => enc x r (q.1,e q.2)) =\n      canonical (fw a b z) (wenc a b x r) := fun x r => funext fun q => by simp [henc]\n  have hm0 : ∀ x r s, enc x r (mark (0,s)) = xor r s := by\n    intro x r s\n    rw [henc,hmk]\n    simp [canonical,owner,keyT,sign,fw,wenc]\n  have hmi : ∀ x r (i : Fin v) s, enc x r (mark (i.succ,s)) = xor (xor (x i) r) s := by\n    intro x r i s\n    rw [henc,hmk]\n    simp [canonical,owner,keyT,sign,fw,wenc]\n  have hinj : ∀ x r x' r', enc x r = enc x' r' → x = x' ∧ r = r' := by\n    intro x r x' r' h\n    have h0 := hm0 x r false\n    rw [h,hm0] at h0\n    simp at h0\n    refine ⟨funext fun i => ?_,h0.symm⟩\n    have h1 := hmi x r i false\n    rw [h,hmi,h0] at h1\n    simpa using h1.symm\n  let decW : Y → X × Bool := fun y =>\n    (fun i => xor (W (fun q => y (q.1,e q.2)) (.inl i.succ)) (W (fun q => y (q.1,e q.2)) (.inl 0)),\n      W (fun q => y (q.1,e q.2)) (.inl 0))\n  obtain ⟨dec,hd1,hd2⟩ : ∃ dec : Y → X × Bool, (∀ x r, dec (enc x r) = (x,r)) ∧\n      ∀ y, (¬ ∃ q : X × Bool, enc q.1 q.2 = y) → dec y = decW y := by\n    refine ⟨fun y => if h : ∃ q : X × Bool, enc q.1 q.2 = y then Classical.choose h else decW y,\n      fun x r => ?_,fun y h => dif_neg h⟩\n    have h : ∃ q : X × Bool, enc q.1 q.2 = enc x r := ⟨(x,r),rfl⟩\n    dsimp only\n    rw [dif_pos h]\n    obtain ⟨h1,h2⟩ := hinj _ _ _ _ (Classical.choose_spec h)\n    exact Prod.ext h1 h2\n  have hbad : ∀ x : X, bad x = ∑ j, diff (x (z j)) (!(x (a j) && x (b j))) := fun x => by\n    simp only [bad,card_filter]\n    rfl\n  refine ⟨e.symm.trans (blue.trans e),e.symm.trans ((green (Ew v g) p).trans e),mark,enc,dec,?_⟩\n  intro cost\n  have hc : ∀ y, cost y = siteCost (Ew v g) p (fun q => y (q.1,e q.2)) := fun y => cost_tr e (Ew v g) p y\n  have hcost : ∀ x r, cost (enc x r) = 6*g + 2*bad x := by\n    intro x r\n    rw [hc,hE x r,layout_exact _ _ _ hp,target_eq,hbad,mul_sum]\n    have h2 : ∀ j : Fin g, 2 * gc (wenc a b x r (.inl (a j).succ)) (wenc a b x r (.inl (b j).succ))\n        (wenc a b x r (.inl (z j).succ)) (wenc a b x r (.inr j)) (wenc a b x r (.inl 0)) =\n        6 + 2 * diff (x (z j)) (!(x (a j) && x (b j))) := by\n      intro j\n      have := gc_exact (x (a j)) (x (b j)) (x (z j)) r\n      simp only [wenc,Matrix.cons_val_succ,Matrix.cons_val_zero,Bool.false_xor]\n      omega\n    simp only [h2,sum_add_distrib,sum_const,card_univ,Fintype.card_fin,smul_eq_mul]\n    ring\n  have hcard : ∀ x r, (univ.filter (fun q : V => enc x r q = true)).card =\n      Fintype.card (Sites (Wq v g)) := by\n    intro x r\n    rw [card_tr (Equiv.prodCongr (Equiv.refl Bool) e)]\n    simpa [henc] using canon_card (fw a b z) (wenc a b x r)\n  have hrep : ∀ y : Y, (univ.filter (fun q : V => y q ≠ enc (dec y).1 (dec y).2 q)).card +\n      2000000*(6*g + 2*bad (dec y).1) ≤ 2000000*cost y := by\n    intro y\n    by_cases hy : ∃ q : X × Bool, enc q.1 q.2 = y\n    · obtain ⟨⟨x,r⟩,rfl⟩ := hy\n      simp [hd1,hcost]\n    · rw [hd2 y hy]\n      obtain ⟨x,r,hdw,hx,hr0⟩ : ∃ x r, decW y = (x,r) ∧\n          (∀ i : Fin v, W (fun q => y (q.1,e q.2)) (.inl i.succ) = xor (x i) r) ∧\n          W (fun q => y (q.1,e q.2)) (.inl 0) = r := ⟨_,_,rfl,fun i => (xor_cancel _ _).symm,rfl⟩\n      rw [hdw]\n      dsimp only\n      have hA : (univ.filter (fun q : V => y q ≠ enc x r q)).card = distance\n          (fun q : Bool × Sites (Wq v g) => y (q.1,e q.2)) (fun q => enc x r (q.1,e q.2)) :=\n        dist_tr e y (enc x r)\n      rw [hE x r] at hA\n      have h := hW (fun q => y (q.1,e q.2))\n      dsimp only at h\n      obtain ⟨h1,h2⟩ := h\n      obtain ⟨E1,D1⟩ := rep_lemma a b z _ x r hr0 hx\n      have hB := dist_tri (fun q : Bool × Sites (Wq v g) => y (q.1,e q.2))\n        (canonical (fw a b z) (W (fun q => y (q.1,e q.2)))) (canonical (fw a b z) (wenc a b x r))\n      have hC := canon_dist (fw a b z) (W (fun q => y (q.1,e q.2))) (wenc a b x r)\n      rw [hbad,hc]\n      omega\n  refine ⟨fun _ => by rw [hmk],fun x r => ⟨hd1 x r,fun s => (hm0 x r s).trans (hadd r s).symm,\n    fun i s => by rw [hmi,hadd,hadd],hcard x r,hcost x r⟩,hrep,fun pins => ?_⟩\n  have key : ∀ y : Y, (cost y = 6*g ∧ y (mark (0,false)) = false ∧ ∀ (i : Fin v) (s : Bool),\n      pins i = some s → y (mark (i.succ,s)) = false) → y = enc (dec y).1 false ∧\n      bad (dec y).1 = 0 ∧ ∀ (i : Fin v) (s : Bool), pins i = some s → (dec y).1 i = s := by\n    rintro y ⟨hy,hm,hpin⟩\n    have h := hrep y\n    rw [hy] at h\n    have h0 : (univ.filter fun q : V => y q ≠ enc (dec y).1 (dec y).2 q).card = 0 := by omega\n    rw [card_eq_zero,filter_eq_empty_iff] at h0\n    have h1 : y = enc (dec y).1 (dec y).2 := funext fun q => not_not.mp (h0 (mem_univ q))\n    rw [h1,hm0] at hm\n    simp at hm\n    refine ⟨by rw [← hm]; exact h1,by omega,fun i s hs => ?_⟩\n    have h4 := hpin i s hs\n    rw [h1,hmi,hm] at h4\n    revert h4\n    cases (dec y).1 i <;> cases s <;> simp\n  apply card_nbij' (fun y => (dec y).1) (fun x => enc x false)\n  · intro y hy\n    simp only [mem_coe,mem_filter,mem_univ,true_and] at hy ⊢\n    exact (key y hy).2\n  · intro x hx\n    simp only [mem_coe,mem_filter,mem_univ,true_and] at hx ⊢\n    refine ⟨by simp [hcost,hx.1],by simp [hm0],fun i s hs => ?_⟩\n    rw [hmi,hx.2 i s hs]\n    cases s <;> rfl\n  · intro y hy\n    simp only [mem_coe,mem_filter,mem_univ,true_and] at hy\n    exact (key y hy).1.symm\n  · intro x _\n    simp only [hd1]\nend PEqualNP.Outside.L_pcx_409\n","lean_bytes":56047,"lean_sha256":"842cbf82bb90aae41522b0d1aa511a1b46d9a186f3c320f8f637d6146e1a34a6","build":{"command":"lake build PEqualNP.Outside.L_pcx_409","exit":0,"seconds":40.97,"peak_memory_gb":7.2},"checker":{"exit":0,"last_line":"ACCEPTED (check): PEqualNP.Outside.L_pcx_409; axioms [propext, Quot.sound, Classical.choice]; kernel re-checked 1963 constants of [PEqualNP.Defs.Base, PEqualNP.Defs.Classes, PEqualNP.Defs.CircuitLowerBounds, PEqualNP.Defs.MetaComplexity, PEqualNP.Defs.MetaComplexityLocal, PEqualNP.Defs.MetaComplexitySparse, PEqualNP.Defs.MetaComplexityMinimal, PEqualNP.Defs.MetaComplexityLgp, PEqualNP.Defs.MetaComplexityLgpMono, PEqualNP.Defs.BinaryCircuit, PEqualNP.Defs.MetaComplexityCoverCircuit, PEqualNP.Defs.ProofComplexity, PEqualNP.Defs.ProofComplexityLin, PEqualNP.Defs.ProofComplexityLinK, PEqualNP.Defs"},"axioms":["propext","Quot.sound","Classical.choice"],"missing":"","unit_status":"At the start (row updated_at 2026-11-27T23:17:13Z) the row listed L-pcx-409 as open: pr_open 0, not checked or withdrawn, and the statement SHA-256 matched the partial. A refetch after the proof was accepted (row updated_at 2026-11-27T23:44:40Z) returned gaps [] and withdrawn {L-pcx-409: proved}. The unit was closed by someone else while this work ran. No lease, sign-in or pull request was made.","approach":"Finished the prove-a partial (NAND identities, permutation expander, matching layout kept). Wires: Q = Fin (v+1) + Fin g x Fin 11; each gate has 11 wire copies forming triangles (a,b,h),(a,h,ref),(b,h,ref) as sign-flipping 3-cycles of a fixed permutation of Fin 11 x Bool, plus an equality link z-h, so target = sum of 2*gc = 6g+2bad at correct h. Sites are carried to Fin n by Fintype.equivFin; sigma, tau are the blue and green matchings conjugated, n = 143364*(v+1+11g). enc is the canonical assignment, dec inverts enc on its image and otherwise rounds via layout_round. Repair: the distance to the rounded w is at most 40000*I, fixing private h bits costs at most 3154008 per wrong gate, and gc_repair pays 2 per wrong h. With C = 2000000, cost 6g forces distance 0 and bad 0; the zero reference pin fixes r, which gives a bijection of the pinned sets."}