import Mathlib.Tactic namespace 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 := by classical calc ∑ i ∈ target, d i ≤ ∑ i ∈ target, ∑ j ∈ starts, (if i ∈ block j then d i else 0) := by apply Finset.sum_le_sum intro i hi obtain ⟨j, hj, hm⟩ := h i hi have 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]) hj simpa [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 := by apply Finset.sum_le_sum intro j _ rw [← Finset.sum_filter] apply Finset.sum_le_sum_of_subset_of_nonneg · intro i hi exact (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 := by classical intro n by_contra failed push_neg at failed let covered := fun s : ℕ => ∀ i ≥ s, ∃ j, i ∈ block j have ex : ∃ s, covered s := ⟨n, failed⟩ let s := Nat.find ex have tail : covered s := Nat.find_spec ex obtain ⟨l, hl⟩ := excess s have mass := cover_mass d hd (Finset.Icc s l) (Finset.Icc s l) block (by intro i hi obtain ⟨hsi, hil⟩ := Finset.mem_Icc.mp hi obtain ⟨j, hj⟩ := tail i hsi have hji := left j i hj have hsj : s ≤ j := by by_contra! small have pred : s - 1 ∈ block j := interval j i (s - 1) hj (by omega) (by omega) have prev : covered (s - 1) := by intro x hx by_cases hsx : s ≤ x · exact tail x hsx · have hx' : x = s - 1 := by omega exact ⟨j, hx' ▸ pred⟩ have minimal : s ≤ s - 1 := Nat.find_min' ex prev omega exact ⟨j, Finset.mem_Icc.mpr ⟨hsj, by omega⟩, hj⟩) have cap := Finset.sum_le_sum (s := Finset.Icc s l) (fun j _ => cost j) linarith theorem remaining_after_allocation (block used need : ℝ) (hblock : 16 * need ≤ block) (hused : used ≤ 6 * need) (hn : 0 ≤ need) : 2 * need ≤ block - used := by linarith theorem greedy_overshoot (used last need : ℝ) (hu : used < 2 * need) (hl : last < 4 * need) : used + last < 6 * need := by linarith theorem floor_capacity (x : ℝ) (hx : 2 ≤ x) : x / 2 ≤ (⌊x⌋ : ℤ) := by have h := Int.lt_floor_add_one x linarith end DecayRamseyPacking