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 for i in 1..D: for j in 1..N: dp[i][j] = q for j in 1..N: for k in 1..N: if j == k: continue if dp[i-1][k] - P[j] >= t: dp[i][j] = max(dp[i][j], dp[i-1][k] - P[j] + Q[j]) var f = false for i in 1..N: if dp[D][i] > q: f = true if f: l = t else: r = t echo l