{"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\nopen Finset\nnamespace PEqualNP.Outside.L_pcx_409\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)\ndef gateCost (a b z h : Bool) : ℕ :=\n  triangle a b h + triangle a h false + triangle b h false + diff z h\ntheorem gate_exact (a b z : Bool) :\n    gateCost a b z (!(a && b)) = 3 + diff z (!(a && b)) := by\n  cases a <;> cases b <;> cases z <;> decide\ntheorem gate_repair (a b z h : Bool) :\n    3 + diff z (!(a && b)) + diff h (!(a && b)) ≤ gateCost a b z h := by\n  cases a <;> cases b <;> cases z <;> cases h <;> decide\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]\nend Layout\ntheorem card_ne {A : Type} [Fintype A] (P R : A → Bool) :\n    (univ.filter (fun i => P i ≠ R i)).card = ∑ i, diff (P i) (R i) := by\n  rw [card_filter]; rfl\ntheorem card_eq {A : Type} [Fintype A] (P R : A → Bool) :\n    (univ.filter (fun i => P i = R i)).card = ∑ i, diff (P i) (!R i) := by\n  rw [card_filter]; exact sum_congr rfl fun i _ => by cases P i <;> cases R i <;> rfl\ntheorem dist_tri {A : Type} [Fintype A] (x y c : A → Bool) :\n    distance x c ≤ distance x y + distance y c := by\n  unfold distance; rw [← sum_add_distrib]; exact sum_le_sum fun i _ => diff_tri _ _ _\nsection Glue\nvariable {Q W : Type} [Fintype Q]\ndef flipS : 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 flipS_inv : Function.Involutive (flipS (Q := Q)) := by\n  rintro (⟨⟨_,_⟩,_⟩|⟨⟨_,_⟩,_⟩) <;> simp [flipS]\ntheorem owner_sum (φ : Q → ℕ) :\n    ∑ p : Bool × Sites Q, φ (owner p.2) = 286728 * ∑ q, φ q := by\n  simp only [Fintype.sum_prod_type, Fintype.sum_sum_type, Fintype.sum_bool, owner, keyT, keyK,\n    sum_const, card_univ, Fintype.card_option, Fintype.card_fin, smul_eq_mul, ← sum_mul,\n    ← sum_add_distrib, Fintype.card_bool, mul_sum]\n  exact sum_congr rfl fun q _ => by ring\ntheorem canon_card {n : ℕ} (e : Sites Q ≃ Fin n) (f : Q → W) (w : W → Bool) :\n    (univ.filter (fun p : Bool × Fin n => canonical f w (p.1, e.symm p.2) = true)).card = n := by\n  let F : Bool × Fin n ≃ Bool × Fin n := Equiv.prodCongr (Equiv.refl _)\n    ((e.symm.trans (Function.Involutive.toPerm _ flipS_inv)).trans e)\n  have h1 := card_filter_add_card_filter_not (s := univ)\n    (fun p : Bool × Fin n => canonical f w (p.1, e.symm p.2) = true)\n  have h2 : (univ.filter (fun p : Bool × Fin n => canonical f w (p.1, e.symm p.2) = true)).card =\n      (univ.filter (fun p : Bool × Fin n => ¬ canonical f w (p.1, e.symm p.2) = true)).card := by\n    refine card_equiv F fun p => ?_\n    have ho : owner (flipS (e.symm p.2)) = owner (e.symm p.2) := by\n      rcases e.symm p.2 with (⟨⟨_,_⟩,_⟩|⟨⟨_,_⟩,_⟩) <;> rfl\n    have hs : sign (flipS (e.symm p.2)) = !sign (e.symm p.2) := by\n      rcases e.symm p.2 with (⟨⟨_,_⟩,_⟩|⟨⟨_,_⟩,_⟩) <;> rfl\n    have hc : canonical f w (p.1, flipS (e.symm p.2)) = !canonical f w (p.1, e.symm p.2) := by\n      simp only [canonical]; rw [ho, hs]; cases w (f (owner (e.symm p.2))) <;> cases sign (e.symm p.2) <;> rfl\n    simp only [F, mem_filter, mem_univ, true_and, Equiv.prodCongr_apply, Prod.map,\n      Equiv.trans_apply, Equiv.symm_apply_apply, Equiv.refl_apply, Function.Involutive.coe_toPerm, hc]\n    cases canonical f w (p.1, e.symm p.2) <;> simp\n  rw [card_univ, Fintype.card_prod, Fintype.card_bool, Fintype.card_fin] at h1\n  omega\ntheorem cost_transfer {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_ne, card_ne, card_eq, ← Equiv.sum_comp e, ← Equiv.sum_comp e, ← Equiv.sum_comp e]\n  simp [siteCost]\ntheorem dist_transfer {n : ℕ} (e : Sites Q ≃ Fin n) (y : Bool × Fin n → Bool)\n    (c : Bool × Sites Q → Bool) :\n    (univ.filter (fun p : Bool × Fin n => y p ≠ c (p.1, e.symm p.2))).card =\n    distance (fun q => y (q.1, e q.2)) c := by\n  rw [card_ne, distance, ← Equiv.sum_comp (Equiv.prodCongr (Equiv.refl Bool) e)]\n  simp [Prod.map]\nend Glue\nabbrev WW (v g : ℕ) := Fin (v+1) ⊕ Fin g\nabbrev QQ (v g : ℕ) := Fin (v+1) ⊕ Fin g × Fin 11\ndef role : Fin 11 → Fin 5 := ![0,1,3,0,3,4,1,3,4,2,3]\ndef gpF (x : Fin 11 × Bool) : Fin 11 × Bool :=\n  if h : x.1.val < 9 then (⟨x.1.val/3*3+(x.1.val%3+1)%3, by omega⟩, !x.2)\n  else if x.2 then x else (⟨19-x.1.val, by have := x.1.isLt; omega⟩, false)\ndef gpI (x : Fin 11 × Bool) : Fin 11 × Bool :=\n  if h : x.1.val < 9 then (⟨x.1.val/3*3+(x.1.val%3+2)%3, by omega⟩, !x.2)\n  else if x.2 then x else (⟨19-x.1.val, by have := x.1.isLt; omega⟩, false)\ndef gpPerm : Equiv.Perm (Fin 11 × Bool) where\n  toFun := gpF\n  invFun := gpI\n  left_inv := by decide\n  right_inv := by decide\ntheorem role_sum (φ : Fin 5 → ℕ) :\n    ∑ k : Fin 11, φ (role k) = 2*φ 0 + 2*φ 1 + φ 2 + 4*φ 3 + 2*φ 4 := by\n  simp [Fin.sum_univ_succ, role]; ring\ntheorem gate_sum (val : Fin 5 → Bool) :\n    ∑ x : Fin 11 × Bool, diff (val (role x.1)) (xor (val (role (gpF x).1)) (xor x.2 (gpF x).2)) =\n    2 * gateCost (xor (val 0) (val 4)) (xor (val 1) (val 4)) (xor (val 2) (val 4))\n      (xor (val 3) (val 4)) := by\n  have h : val = ![val 0, val 1, val 2, val 3, val 4] := by funext m; fin_cases m <;> rfl\n  rw [h]\n  generalize val 0 = A; generalize val 1 = B; generalize val 2 = Z\n  generalize val 3 = H; generalize val 4 = R\n  revert A B Z H R; decide\nsection Wiring\nvariable {v g : ℕ} (a b z : Fin g → Fin v)\ndef wire (j : Fin g) : Fin 5 → WW v g :=\n  ![.inl (a j).succ, .inl (b j).succ, .inl (z j).succ, .inr j, .inl 0]\ndef fq : QQ v g → WW v g := Sum.elim Sum.inl (fun p => wire a b z p.1 (role p.2))\ndef phi : QQ v g × Bool ≃ (Fin (v+1) × Bool) ⊕ Fin g × (Fin 11 × Bool) :=\n  (Equiv.sumProdDistrib _ _ _).trans (Equiv.sumCongr (Equiv.refl _) (Equiv.prodAssoc _ _ _))\ndef EE : Equiv.Perm (QQ v g × Bool) :=\n  (phi.trans (Equiv.sumCongr (Equiv.refl _) (Equiv.prodCongr (Equiv.refl _) gpPerm))).trans phi.symm\ntheorem target_eq (w : WW v g → Bool) :\n    target EE (fq a b z) w = 2 * ∑ j, gateCost (xor (w (.inl (a j).succ)) (w (.inl 0)))\n      (xor (w (.inl (b j).succ)) (w (.inl 0))) (xor (w (.inl (z j).succ)) (w (.inl 0)))\n      (xor (w (.inr j)) (w (.inl 0))) := by\n  unfold target extErr\n  rw [← Equiv.sum_comp phi.symm, Fintype.sum_sum_type, sum_eq_zero (fun y _ => ?_), zero_add,\n    Fintype.sum_prod_type, mul_sum]\n  · exact sum_congr rfl fun j _ => gate_sum (fun m => w (wire a b z j m))\n  · show diff (w (fq a b z (.inl y.1))) (xor (w (fq a b z (.inl y.1))) (xor y.2 y.2)) = 0\n    simp [diff_self]\ndef wv (x : Fin v → Bool) (r : Bool) : WW v g → Bool :=\n  Sum.elim (fun i => xor ((Fin.cons false x : Fin (v+1) → Bool) i) r)\n    (fun j => xor (!(x (a j) && x (b j))) r)\ntheorem target_wv (x : Fin v → Bool) (r : Bool) :\n    target EE (fq a b z) (wv a b x r) =\n      6*g + 2*(univ.filter (fun j : Fin g => x (z j) ≠ !(x (a j) && x (b j)))).card := by\n  rw [target_eq, card_ne]\n  simp only [wv, Sum.elim_inl, Sum.elim_inr, Fin.cons_zero, Fin.cons_succ, Bool.false_xor,\n    xor_cancel, gate_exact, sum_add_distrib, sum_const, card_univ, Fintype.card_fin, smul_eq_mul]\n  ring\ntheorem dist_canon (w w' : WW v g → Bool) (hw : ∀ i, w (.inl i) = w' (.inl i)) :\n    distance (canonical (fq a b z) w) (canonical (fq a b z) w') =\n      1146912 * ∑ j, diff (w (.inr j)) (w' (.inr j)) := by\n  have h1 : distance (canonical (fq a b z) w) (canonical (fq a b z) w') =\n      286728 * ∑ q, diff (w (fq a b z q)) (w' (fq a b z q)) := by\n    rw [← owner_sum]; unfold distance canonical; exact sum_congr rfl fun p _ => diff_xor _ _ _\n  rw [h1, Fintype.sum_sum_type, Fintype.sum_prod_type]\n  simp only [fq, Sum.elim_inl, Sum.elim_inr, hw, diff_self, sum_const_zero, zero_add, role_sum,\n    mul_sum]\n  refine sum_congr rfl fun j _ => ?_\n  have h2 := role_sum (fun m => diff (w (wire a b z j m)) (w' (wire a b z j m)))\n  rw [← mul_sum, h2]; simp [wire, hw, diff_self]; ring\nend Wiring\ntheorem size_bound (v g : ℕ) : Fintype.card (Sites (QQ v g)) ≤ 2000000*(v+g+1) := by\n  simp only [Fintype.card_sum, Fintype.card_prod, Fintype.card_fin, Fintype.card_option,\n    Fintype.card_bool]\n  nlinarith\nend PEqualNP.Outside.L_pcx_409\nopen PEqualNP.Outside.L_pcx_409\ntheorem 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  refine ⟨2000000, by norm_num, fun v g a b z => ?_⟩\n  intro X add bad\n  obtain ⟨pn, hp, hr⟩ := layout_round (EE (v:=v) (g:=g)) (fq a b z)\n  choose wr hwr using hr\n  refine ⟨Fintype.card (Sites (QQ v g)), size_bound v g, ?_⟩\n  intro V Y\n  let e : Sites (QQ v g) ≃ Fin (Fintype.card (Sites (QQ v g))) := Fintype.equivFin _\n  let toA : Y → Assignment (QQ v g) := fun y q => y (q.1, e q.2)\n  let enc : X → Bool → Y := fun x r p => canonical (fq a b z) (wv a b x r) (p.1, e.symm p.2)\n  let rd : Y → X × Bool := fun y =>\n    (fun i => xor (wr (toA y) (.inl i.succ)) (wr (toA y) (.inl 0)), wr (toA y) (.inl 0))\n  let dec : Y → X × Bool := fun y =>\n    if h : ∃ q : X × Bool, enc q.1 q.2 = y then h.choose else rd y\n  have hm : ∀ x r (i : Fin (v+1)) s,\n      enc x r (false, e (.inl ((((.inl i : QQ v g),false),s),none))) = xor (wv a b x r (.inl i)) s := by\n    intro x r i s; simp [enc, canonical, owner, keyT, sign, fq]\n  let mark : (Fin (v+1) × Bool) ↪ V :=\n    ⟨fun q => (false, e (.inl ((((.inl q.1 : QQ v g),false),q.2),none))), by\n      rintro ⟨i,s⟩ ⟨i',s'⟩ h\n      have := e.injective (congrArg Prod.snd h)\n      simp at this\n      obtain ⟨rfl, rfl⟩ := this; rfl⟩\n  have hadd : ∀ r s : Bool, add r s = xor r s := by intro r s; cases r <;> cases s <;> rfl\n  have h0 : ∀ x r s, enc x r (mark (0,s)) = add r s := by\n    intro x r s; rw [hadd]; exact (hm x r 0 s).trans (by simp [wv])\n  have h1 : ∀ x r (i : Fin v) s, enc x r (mark (i.succ,s)) = add (add (x i) r) s := by\n    intro x r i s; rw [hadd, hadd]; exact (hm x r i.succ s).trans (by simp [wv])\n  have hA : ∀ x r, toA (enc x r) = canonical (fq a b z) (wv a b x r) := by\n    intro x r; funext q; simp [toA, enc]\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 hr : r = r' := by\n      have := congrFun h (mark (0,false)); rw [h0, h0, hadd, hadd] at this\n      revert this; cases r <;> cases r' <;> decide\n    subst hr\n    refine ⟨funext fun i => ?_, rfl⟩\n    have := congrFun h (mark (i.succ,false)); rw [h1, h1, hadd, hadd, hadd, hadd] at this\n    revert this; cases x i <;> cases x' i <;> cases r <;> decide\n  have hdec : ∀ x r, dec (enc x r) = (x,r) := by\n    intro x r\n    have h : ∃ q : X × Bool, enc q.1 q.2 = enc x r := ⟨(x,r), rfl⟩\n    show (if h : ∃ q : X × Bool, enc q.1 q.2 = enc x r then h.choose else rd (enc x r)) = (x,r)\n    rw [dif_pos h]\n    obtain ⟨e1, e2⟩ := hinj _ _ _ _ h.choose_spec\n    exact Prod.ext e1 e2\n  refine ⟨(e.symm.trans blue).trans e, (e.symm.trans (green EE pn)).trans e, mark, enc, dec, ?_⟩\n  intro cost\n  have hcost : ∀ y, cost y = siteCost EE pn (toA y) := fun y => cost_transfer e EE pn y\n  have hce : ∀ x r, cost (enc x r) = 6*g + 2*bad x := by\n    intro x r; rw [hcost, hA, layout_exact _ _ _ hp, target_wv]\n  have hrep : ∀ y : Y, (univ.filter (fun p : V => y p ≠ enc (dec y).1 (dec y).2 p)).card +\n      2000000*(6*g + 2*bad (dec y).1) ≤ 2000000*cost y := by\n    intro y\n    by_cases h : ∃ q : X × Bool, enc q.1 q.2 = y\n    · obtain ⟨⟨x,r⟩, rfl⟩ := h\n      rw [hdec, hce]; simp\n    · have hd : dec y = rd y := dif_neg h\n      rw [hd, hcost]\n      have hdt : (univ.filter (fun p : V => y p ≠ enc (rd y).1 (rd y).2 p)).card =\n          distance (toA y) (canonical (fq a b z) (wv a b (rd y).1 (rd y).2)) := dist_transfer e y (canonical (fq a b z) (wv a b (rd y).1 (rd y).2))\n      have hh := hwr (toA y)\n      dsimp only at hh\n      obtain ⟨hw1, hw2⟩ := hh\n      have hg : 2*(∑ j, (3 + diff ((rd y).1 (z j)) (!((rd y).1 (a j) && (rd y).1 (b j))) +\n          diff (xor (wr (toA y) (.inr j)) (wr (toA y) (.inl 0))) (!((rd y).1 (a j) && (rd y).1 (b j))))) ≤\n          target EE (fq a b z) (wr (toA y)) := by\n        rw [target_eq]\n        exact Nat.mul_le_mul_left _ (sum_le_sum fun j _ => gate_repair ((rd y).1 (a j)) ((rd y).1 (b j))\n          ((rd y).1 (z j)) (xor (wr (toA y) (.inr j)) (wr (toA y) (.inl 0))))\n      rw [sum_add_distrib, sum_add_distrib, sum_const, card_univ, Fintype.card_fin, smul_eq_mul] at hg\n      have hB : ∑ j, diff (xor (wr (toA y) (.inr j)) (wr (toA y) (.inl 0))) (!((rd y).1 (a j) && (rd y).1 (b j))) =\n          ∑ j, diff (wr (toA y) (.inr j)) (wv a b (rd y).1 (rd y).2 (.inr j)) :=\n        sum_congr rfl fun j _ => by rw [← diff_xor _ _ (wr (toA y) (.inl 0)), xor_cancel]; rfl\n      have hb : bad (rd y).1 = ∑ j, diff ((rd y).1 (z j)) (!((rd y).1 (a j) && (rd y).1 (b j))) := card_ne _ _\n      have hag : ∀ i, wr (toA y) (.inl i) = wv a b (rd y).1 (rd y).2 (.inl i) := by\n        intro i\n        refine Fin.cases ?_ (fun i => ?_) i\n        · simp [wv, rd]\n        · simp only [wv, rd, Sum.elim_inl, Fin.cons_succ, xor_cancel]\n      have hdc := dist_canon a b z (wr (toA y)) (wv a b (rd y).1 (rd y).2) hag\n      have htri := dist_tri (toA y) (canonical (fq a b z) (wr (toA y)))\n        (canonical (fq a b z) (wv a b (rd y).1 (rd y).2))\n      rw [hdt, hb]\n      omega\n  refine ⟨fun q => rfl, fun x r => ⟨hdec x r, h0 x r, h1 x r, canon_card e (fq a b z) (wv a b x r),\n    hce x r⟩, hrep, fun pins => ?_⟩\n  have key : ∀ y ∈ 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),\n      bad (dec y).1 = 0 ∧ enc (dec y).1 false = y := by\n    intro y hy\n    simp only [mem_filter, mem_univ, true_and] at hy\n    have hh := hrep y\n    rw [hy.1] at hh\n    have hd : (univ.filter (fun p : V => y p ≠ enc (dec y).1 (dec y).2 p)).card = 0 := by omega\n    have he : enc (dec y).1 (dec y).2 = y := funext fun p => by\n      by_contra hne\n      exact card_ne_zero.mpr ⟨p, mem_filter.mpr ⟨mem_univ _, fun h => hne h.symm⟩⟩ hd\n    have hr : (dec y).2 = false := by\n      have := congrFun he (mark (0,false)); rw [h0, hy.2.1, hadd] at this\n      revert this; cases (dec y).2 <;> decide\n    rw [hr] at he\n    exact ⟨by omega, he⟩\n  symm\n  refine card_nbij' (fun x => enc x false) (fun y => (dec y).1) ?_ ?_ ?_ ?_\n  · intro x hx\n    simp only [coe_filter, Set.mem_setOf_eq, mem_filter, mem_univ, true_and] at hx ⊢\n    refine ⟨by rw [hce, hx.1]; rfl, by rw [h0]; rfl, fun i s hs => ?_⟩\n    rw [h1, hx.2 i s hs]; cases s <;> rfl\n  · intro y hy\n    have hk := key y hy\n    simp only [coe_filter, Set.mem_setOf_eq, mem_filter, mem_univ, true_and] at hy ⊢\n    refine ⟨hk.1, fun i s hs => ?_⟩\n    have := hy.2.2 i s hs\n    rw [← hk.2, h1, hadd, hadd] at this\n    revert this; cases (dec y).1 i <;> cases s <;> decide\n  · intro x _; simp only [hdec]\n  · intro y hy; exact (key y hy).2\n","lean_bytes":57059,"lean_sha256":"65c658f512ecad773361703845c34ad131c635fbc808ed02541f55aead185b4c","build":{"exit":0,"seconds":22.1,"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 1961 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.ProofComplexityLift, PEqualNP.Defs.ProofComplexityResK, PEqualNP.Defs.ProofComplexityRestrict, PEqualNP.Defs.ProofComplexityBit, PEqualNP.Defs.ProofComplexityPC, PEqualNP.Defs.ProofComplexityMoment, PEqualNP.Defs.ProofComplexityChain, PEqualNP.Defs.ProofComplexityGeneral, PEqualNP.Defs.Search, PEqualNP.Defs.SearchQ, PEqualNP.Defs.SearchQTree, PEqualNP.Defs.SearchCP, PEqualNP.Defs.SearchRUP, PEqualNP.Defs.SearchQSyn, PEqualNP.Defs.SearchSoS, PEqualNP.Defs.Gct, PEqualNP.Defs.Oracle, PEqualNP.Defs.Algebraic, PEqualNP.Defs.Expanders, PEqualNP.Defs.RiffleRank, PEqualNP.Defs.TimeAlt, PEqualNP.Defs.CoverPromise, PEqualNP.Defs.SingleLevel, PEqualNP.Defs.DeMorganFormula, PEqualNP.Defs.MetaComplexityTypedRank, PEqualNP.Defs.Pebble, PEqualNP.Defs.Subdivision, PEqualNP.Defs.AndCountCircuit, PEqualNP.Defs.PebbleThreeWay, PEqualNP.Defs.CoLevel, PEqualNP.Defs.Peelable, PEqualNP.Defs.NestedCoverCircuit, PEqualNP.Defs.LiteralPartition, PEqualNP.Defs.MinimumComplex, PEqualNP.Defs.PendantBit, PEqualNP.Defs.BorrowedCoverCircuit, PEqualNP.Defs.LineDegree, PEqualNP.Defs.CircuitMoves, PEqualNP.Defs.LowMerge, PEqualNP.Defs.NewLowMerge, PEqualNP.Defs.AffinePlane, PEqualNP.Defs.EdgeOwnership, PEqualNP.Defs.PencilGate, PEqualNP.Defs.BlindGate, PEqualNP.Defs.BicliqueBlockCost, PEqualNP.Defs.SharingOrder, PEqualNP.Defs.NotPremium, PEqualNP.Defs.NotBracket, PEqualNP.Defs.PairShadow, PEqualNP.Defs.SplitCount, PEqualNP.Defs.InvisibleWire, PEqualNP.Defs.SharedOr, PEqualNP.Defs.CleanedNonEdge, PEqualNP.Defs.MergeCost, PEqualNP.Defs.CarriedProduct, PEqualNP.Defs.TopForm, PEqualNP.Defs.SyntacticMultilinear, PEqualNP.Defs.TopFormNames, PEqualNP.Defs.HeptadCover, PEqualNP.Defs.ReadK, PEqualNP.Defs.GenericRejection, PEqualNP.Defs.CrossAnd, PEqualNP.Defs.ReadKJoin, PEqualNP.Defs.SharedBlockCost, PEqualNP.Defs.ReadTwo, PEqualNP.Defs.LratEncoding, PEqualNP.Defs.Core, PEqualNP.Defs.PointTypes, PEqualNP.Defs.FactorSubst, PEqualNP.Defs.TwoColour, PEqualNP.Defs.Represents, PEqualNP.Defs.BranchingProgram, PEqualNP.Defs, PEqualNP.Outside.L_pcx_409]"},"axioms":["propext","Quot.sound","Classical.choice"],"missing":"","approach":"Kept the partial (NAND identities, permutation expander, matching layout). Wires W = reference+variables (Fin (v+1)) plus one private output per gate. Keys Q = one anchor per reference/variable plus 11 keys per gate; E acts per gate as a fixed permutation of Fin 11 x Bool: each of the triangles (a,b,h),(a,h,ref),(b,h,ref) is a 6-cycle with sign flips, so each disagreement is counted twice, and z,h form one equal-sign swap. So target = 2*sum gateCost, giving cost 6g+2bad. n = card Sites = 143364*(v+1+11g), C = 2000000. enc is the canonical layout; dec returns the preimage if y is an encoding, otherwise it rounds with layout_round and rebuilds the gate outputs. Fixing the outputs moves 4 keys per gate, a distance of 1146912 per wrong output, paid for by gate_repair. Zero cost-6g slack forces distance 0. card_nbij gives the equality of pinned counts."}