import Mathlib.Tacticnamespace DecayRamseyPacking/-- Count a nonnegative weight at least once when finite intervals cover its index. -/theorem cover_mass (d : ℕ → ℝ) (hd : ∀ i, 0 ≤ d i)(target starts : Finset ℕ) (block : ℕ → Finset ℕ)(h : ∀ i ∈ target, ∃ j ∈ starts, i ∈ block j) :∑ i ∈ target, d i ≤ ∑ j ∈ starts, ∑ i ∈ block j, d i := byclassicalcalc∑ i ∈ target, d i ≤ ∑ i ∈ target, ∑ j ∈ starts, (if i ∈ block j then d i else 0) := byapply Finset.sum_le_sumintro i hiobtain ⟨j, hj, hm⟩ := h i hihave hsingle := Finset.single_le_sum(f := fun j => if i ∈ block j then d i else 0)(s := starts) (fun j _ => by by_cases hh : i ∈ block j <;> simp [hh, hd]) hjsimpa [hm] using hsingle_ = ∑ j ∈ starts, ∑ i ∈ target, (if i ∈ block j then d i else 0) := Finset.sum_comm_ ≤ ∑ j ∈ starts, ∑ i ∈ block j, d i := byapply Finset.sum_le_sumintro j _rw [← Finset.sum_filter]apply Finset.sum_le_sum_of_subset_of_nonneg· intro i hiexact (Finset.mem_filter.mp hi).2· intro i _ _exact hd i/-- Arbitrarily large uncovered cutoffs. An unmarked start has an empty block.The finite witness in 'excess' follows from the strict tail inequality. -/theorem unbounded_uncovered (d b : ℕ → ℝ) (hd : ∀ i, 0 ≤ d i)(block : ℕ → Finset ℕ)(left : ∀ j i, i ∈ block j → j ≤ i)(interval : ∀ j i h, i ∈ block j → j ≤ h → h ≤ i → h ∈ block j)(cost : ∀ j, ∑ i ∈ block j, d i ≤ 20 * b j)(excess : ∀ s, ∃ l, ∑ i ∈ Finset.Icc s l, d i > ∑ j ∈ Finset.Icc s l, 20 * b j) :∀ n, ∃ m ≥ n, ∀ j, m ∉ block j := byclassicalintro nby_contra failedpush_neg at failedlet covered := fun s : ℕ => ∀ i ≥ s, ∃ j, i ∈ block jhave ex : ∃ s, covered s := ⟨n, failed⟩let s := Nat.find exhave tail : covered s := Nat.find_spec exobtain ⟨l, hl⟩ := excess shave mass := cover_mass d hd (Finset.Icc s l) (Finset.Icc s l) block (byintro i hiobtain ⟨hsi, hil⟩ := Finset.mem_Icc.mp hiobtain ⟨j, hj⟩ := tail i hsihave hji := left j i hjhave hsj : s ≤ j := byby_contra! smallhave pred : s - 1 ∈ block j := interval j i (s - 1) hj (by omega) (by omega)have prev : covered (s - 1) := byintro x hxby_cases hsx : s ≤ x· exact tail x hsx· have hx' : x = s - 1 := by omegaexact ⟨j, hx' ▸ pred⟩have minimal : s ≤ s - 1 := Nat.find_min' ex prevomegaexact ⟨j, Finset.mem_Icc.mpr ⟨hsj, by omega⟩, hj⟩)have cap := Finset.sum_le_sum (s := Finset.Icc s l) (fun j _ => cost j)linariththeorem remaining_after_allocation (block used need : ℝ)(hblock : 16 * need ≤ block) (hused : used ≤ 6 * need) (hn : 0 ≤ need) :2 * need ≤ block - used := by linariththeorem greedy_overshoot (used last need : ℝ)(hu : used < 2 * need) (hl : last < 4 * need) :used + last < 6 * need := by linariththeorem floor_capacity (x : ℝ) (hx : 2 ≤ x) : x / 2 ≤ (⌊x⌋ : ℤ) := byhave h := Int.lt_floor_add_one xlinarithend DecayRamseyPacking