import sequtils,strutils var N,D: int P, Q: array[1010, int] dp : array[1010, array[1010, int]] var input = stdin.readLine.split.map(parseInt) (N, D) = (input[0], input[1]) for n in 1..N: input = stdin.readLine.split.map(parseInt) (P[n], Q[n]) = (input[0], input[1]) var r = 0 l = -100000001 q = -100000000001 while r - l > 1: var t = (r + l) div 2 var f = false for i in 1..D: f = false for j in 1..N: dp[i][j] = q var k, mk : int mk = q for j in 1..N: if dp[i-1][j] > mk: k = j mk = dp[i-1][j] f = false for j in 1..N: if j == k: continue if mk - P[j] >= t: dp[i][j] = mk - P[j] + Q[j] f = true if dp[i - 1][j] - P[k] >= t: dp[i][k] = max(dp[i - 1][j] - P[k] + Q[k], dp[i][k]) f = true if f == false: break if f: l = t else: r = t echo l