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;
  int n, m, root = 0;
  int val[4000], siz[4000];
  int lc[4000], rc[4000];
  long long int dp[4000][4004];
  int build(int l, int r) {
    if (l > r) return -1;
    int rt = l;
    for (int i = l + 1; i <= r; i++)
      if (val[rt] > val[i]) rt = i;
    lc[rt] = build(l, rt - 1);
    rc[rt] = build(rt + 1, r);
    return rt;
  }
  long long int temp[4004];
  int dfs(int i) {
    if (i == -1) return 0;
    siz[i] = 1 + dfs(lc[i]);
    siz[i] += dfs(rc[i]);
    dp[i][0] = 0;
    dp[i][1] = (long long int)val[i] * (m - 1);
    if (lc[i] != -1) {
      for (int j = 0; j <= n; j++) temp[j] = 0;
      int ma = siz[lc[i]], mb = 1;
      for (int a = 0; a <= ma; a++)
        for (int b = 0; b <= mb; b++)
          temp[a + b] = max(temp[a + b], dp[lc[i]][a] + dp[i][b] -
                                             (long long int)2 * a * b * val[i]);
      for (int j = 0; j <= siz[i]; j++) dp[i][j] = temp[j];
    }
    if (rc[i] != -1) {
      for (int j = 0; j <= n; j++) temp[j] = 0;
      int mb = siz[rc[i]], ma = siz[i] - mb;
      for (int a = 0; a <= ma; a++)
        for (int b = 0; b <= mb; b++)
          temp[a + b] = max(temp[a + b], dp[i][a] + dp[rc[i]][b] -
                                             (long long int)2 * a * b * val[i]);
      for (int j = 0; j <= siz[i]; j++) dp[i][j] = temp[j];
    }
    return siz[i];
  }
  int main(void) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
      scanf("%d", &val[i]);
      if (val[root] > val[i]) root = i;
    }
    lc[root] = build(0, root - 1);
    rc[root] = build(root + 1, n - 1);
    dfs(root);
    printf("%lld\n", dp[root][m]);
    return 0;
  }
  