import sys def main(): import sys n, d = map(int, sys.stdin.readline().split()) dishes = [] for _ in range(n): p, q = map(int, sys.stdin.readline().split()) dishes.append((p, q)) # Initialize for day 1 dp = [{} for _ in range(d + 1)] for j in range(n): p, q = dishes[j] e = -p + q m = -p dp[1][j] = [(e, m)] for day in range(2, d + 1): curr = {} for prev_dish in dp[day - 1]: prev_states = dp[day - 1][prev_dish] for (e_prev, m_prev) in prev_states: for j in range(n): if j == prev_dish: continue p_j, q_j = dishes[j] new_cook = e_prev - p_j new_min = min(m_prev, new_cook) new_e = new_cook + q_j if j not in curr: curr[j] = [] candidates = curr[j] to_add = True to_remove = [] for idx in reversed(range(len(candidates))): e_exist, m_exist = candidates[idx] if e_exist >= new_e and m_exist >= new_min: to_add = False break if new_e >= e_exist and new_min >= m_exist: to_remove.append(idx) if to_add: for idx in to_remove: del candidates[idx] candidates.append((new_e, new_min)) # Check if new entry dominates others new_remove = [] for idx in reversed(range(len(candidates) - 1)): e_exist, m_exist = candidates[idx] if new_e >= e_exist and new_min >= m_exist: new_remove.append(idx) for idx in new_remove: del candidates[idx] else: pass dp[day] = curr max_min = -float('inf') if d == 0: print(0) return for states in dp[d].values(): for (e, m) in states: if m > max_min: max_min = m print(max_min) if __name__ == '__main__': main()