description: Alice has an integer sequence a of length n and all elements are different. She will choose a subsequence of a of length m, and defines the value of a subsequence a_{b_1},a_{b_2},…,a_{b_m} as $$$∑_{i = 1}^m (m ⋅ a_{b_i}) - ∑_{i = 1}^m ∑_{j = 1}^m f(min(b_i, b_j), max(b_i, b_j)), where f(i, j) denotes \min(a_i, a_{i + 1}, \ldots, a_j)$$$.
  
  Alice wants you to help her to maximize the value of the subsequence she choose.
  
  A sequence s is a subsequence of a sequence t if s can be obtained from t by deletion of several (possibly, zero or all) elements.
  
  Input
  
  The first line contains two integers n and m (1 ≤ m ≤ n ≤ 4000).
  
  The second line contains n distinct integers a_1, a_2, …, a_n (1 ≤ a_i < 2^{31}).
  
  Output
  
  Print the maximal value Alice can get.
  
  Examples
  
  Input
  
  
  6 4
  15 2 18 12 13 4
  
  
  Output
  
  
  100
  
  
  Input
  
  
  11 5
  9 3 7 1 8 12 10 20 15 18 5
  
  
  Output
  
  
  176
  
  
  Input
  
  
  1 1
  114514
  
  
  Output
  
  
  0
  
  
  Input
  
  
  2 1
  666 888
  
  
  Output
  
  
  0
  
  Note
  
  In the first example, Alice can choose the subsequence [15, 2, 18, 13], which has the value 4 ⋅ (15 + 2 + 18 + 13) - (15 + 2 + 2 + 2) - (2 + 2 + 2 + 2) - (2 + 2 + 18 + 12) - (2 + 2 + 12 + 13) = 100. In the second example, there are a variety of subsequences with value 176, and one of them is [9, 7, 12, 20, 18].
solution: #include <bits/stdc++.h>
  using namespace std;
  const int NMAX = 4000;
  int N, M, a[NMAX + 2];
  int m[NMAX + 2][NMAX + 2];
  int sz[NMAX + 2];
  int ls[NMAX + 2], lw[NMAX + 2];
  int rs[NMAX + 2], rw[NMAX + 2];
  long long dp[NMAX + 2][NMAX + 2];
  void dfs(int node) {
    sz[node] = 1;
    if (ls[node] != 0) {
      dfs(ls[node]);
      for (int i = min(M, sz[node]); i >= 0; i--) {
        for (int j = min(M, sz[ls[node]]); j >= 0; j--) {
          dp[node][i + j] =
              max(dp[node][i + j],
                  dp[node][i] + dp[ls[node]][j] + 1LL * j * (M - j) * lw[node]);
        }
      }
      sz[node] += sz[ls[node]];
    }
    if (rs[node] != 0) {
      dfs(rs[node]);
      for (int i = min(M, sz[node]); i >= 0; i--) {
        for (int j = min(M, sz[rs[node]]); j >= 0; j--) {
          dp[node][i + j] =
              max(dp[node][i + j],
                  dp[node][i] + dp[rs[node]][j] + 1LL * j * (M - j) * rw[node]);
        }
      }
      sz[node] += sz[rs[node]];
    }
  }
  void build_tree(int l, int r) {
    if (l == r) {
      return;
    }
    int v = m[l][r];
    if (m[l][v - 1] != 0) {
      ls[v] = m[l][v - 1];
      lw[v] = a[m[l][v - 1]] - a[v];
      build_tree(l, v - 1);
    }
    if (m[v + 1][r] != 0) {
      rs[v] = m[v + 1][r];
      rw[v] = a[m[v + 1][r]] - a[v];
      build_tree(v + 1, r);
    }
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> M;
    for (int i = 1; i <= N; i++) {
      cin >> a[i];
    }
    if (M == 1) {
      cout << 0 << '\n';
      return 0;
    }
    for (int i = 1; i <= N; i++) {
      m[i][i] = i;
    }
    for (int l = 2; l <= N; l++) {
      for (int i = 1; i + l - 1 <= N; i++) {
        if (a[m[i][i + l - 2]] < a[m[i + 1][i + l - 1]]) {
          m[i][i + l - 1] = m[i][i + l - 2];
        } else {
          m[i][i + l - 1] = m[i + 1][i + l - 1];
        }
      }
    }
    build_tree(1, N);
    dfs(m[1][N]);
    cout << dp[m[1][N]][M] << '\n';
    return 0;
  }
  