if __name__ == '__main__':
N, k = [int(x) for x in input().split()]
prices = [int(x) for x in input().split()]
# i:天数,j:交易数
# f[i, j, 0] = max(f[i-1, j, 0], f[i-1, j, 1] + w[i])
# f[i, j, 1] = max(f[i-1, j, 1], f[i-1, j-1, 0] - w[i]) 注:此处发生了买入行为,所以是 j - 1
# 最终状态 f[i, j, 0]
f = [[0, -1e20] for _ in range(k + 1)]
for i in range(N):
for j in range(k, 0, -1):
f[j][0], f[j][1] = max(f[j][0], f[j][1] + prices[i]), max(f[j][1], f[j-1][0] - prices[i])
print(f[k][0])